42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
pub mod auth;
|
|
pub mod requests;
|
|
|
|
use dotenv_codegen::dotenv;
|
|
|
|
pub use requests::{request_delete, request_get, request_post, request_put};
|
|
|
|
use gloo_storage::{LocalStorage, Storage};
|
|
use lazy_static::lazy_static;
|
|
use parking_lot::RwLock;
|
|
|
|
const API_ROOT: &str = dotenv!("API_ROOT");
|
|
const TOKEN_KEY: &str = "jheuel-token";
|
|
|
|
lazy_static! {
|
|
/// Jwt token read from local storage.
|
|
pub static ref TOKEN: RwLock<Option<String>> = {
|
|
if let Ok(token) = LocalStorage::get(TOKEN_KEY) {
|
|
RwLock::new(Some(token))
|
|
} else {
|
|
RwLock::new(None)
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Set jwt token to local storage.
|
|
pub fn set_token(token: Option<String>) {
|
|
if let Some(t) = token.clone() {
|
|
LocalStorage::set(TOKEN_KEY, t).expect("failed to set");
|
|
} else {
|
|
LocalStorage::delete(TOKEN_KEY);
|
|
}
|
|
let mut token_lock = TOKEN.write();
|
|
*token_lock = token;
|
|
}
|
|
|
|
/// Get jwt token from lazy static.
|
|
pub fn get_token() -> Option<String> {
|
|
let token_lock = TOKEN.read();
|
|
token_lock.clone()
|
|
}
|