use axum::{extract::FromRequestParts, http::request::Parts}; use std::result; use tower_sessions::{Session, session::Error}; use uuid::Uuid; pub struct TypedSession(Session); type Result = result::Result; impl TypedSession { const USER_ID_KEY: &'static str = "user_id"; const USERNAME_KEY: &'static str = "username"; pub async fn renew(&self) -> Result<()> { self.0.cycle_id().await } pub async fn insert_user_id(&self, user_id: Uuid) -> Result<()> { self.0.insert(Self::USER_ID_KEY, user_id).await } pub async fn get_user_id(&self) -> Result> { self.0.get(Self::USER_ID_KEY).await } pub async fn insert_username(&self, username: String) -> Result<()> { self.0.insert(Self::USERNAME_KEY, username).await } pub async fn get_username(&self) -> Result> { self.0.get(Self::USERNAME_KEY).await } pub async fn clear(&self) { self.0.clear().await; } } impl FromRequestParts for TypedSession where S: Sync + Send, { type Rejection = >::Rejection; async fn from_request_parts( parts: &mut Parts, state: &S, ) -> result::Result { Session::from_request_parts(parts, state) .await .map(TypedSession) } }