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> = { 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) { 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 { let token_lock = TOKEN.read(); token_lock.clone() }