pub struct SessionManager { /* private fields */ }Expand description
Request-scoped session manager, available as an axum extractor.
Inject SessionManager as a handler parameter to read or modify the session
for the current request. The session middleware must be installed via
crate::layer for the extractor to work; if the middleware is missing the
extractor returns an internal error.
Changes made through SessionManager (authentication, logout, token
rotation, data writes) are applied to the HTTP response cookie automatically
by the middleware after the handler returns.
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub async fn authenticate(&self, user_id: &str) -> Result<(), Error>
pub async fn authenticate(&self, user_id: &str) -> Result<(), Error>
Create a session for the given user. Destroys any existing session first (fixation prevention).
Sourcepub async fn authenticate_with(
&self,
user_id: &str,
data: Value,
) -> Result<(), Error>
pub async fn authenticate_with( &self, user_id: &str, data: Value, ) -> Result<(), Error>
Create a session with custom data attached.
Any existing session is destroyed before the new one is created to prevent session-fixation attacks.
Sourcepub async fn logout(&self) -> Result<(), Error>
pub async fn logout(&self) -> Result<(), Error>
Destroy the current session. Cookie is removed automatically.
Sourcepub async fn logout_all(&self) -> Result<(), Error>
pub async fn logout_all(&self) -> Result<(), Error>
Destroy ALL sessions for the current user.
Sourcepub async fn logout_other(&self) -> Result<(), Error>
pub async fn logout_other(&self) -> Result<(), Error>
Destroy all sessions for the current user except the current one.
Sourcepub async fn revoke(&self, id: &SessionId) -> Result<(), Error>
pub async fn revoke(&self, id: &SessionId) -> Result<(), Error>
Destroy a specific session by ID (for “manage my devices” UI). Only works on sessions owned by the current user.
Sourcepub async fn rotate(&self) -> Result<(), Error>
pub async fn rotate(&self) -> Result<(), Error>
Regenerate the session token without changing the session ID.
Sourcepub async fn current(&self) -> Option<SessionData>
pub async fn current(&self) -> Option<SessionData>
Access the current session data (if authenticated).
Sourcepub async fn is_authenticated(&self) -> bool
pub async fn is_authenticated(&self) -> bool
Check if a session is active.
Sourcepub async fn list_my_sessions(&self) -> Result<Vec<SessionData>, Error>
pub async fn list_my_sessions(&self) -> Result<Vec<SessionData>, Error>
List all active sessions for the authenticated user.
Sourcepub async fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, Error>
pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, Error>
Get a typed value from the session data by key.
Trait Implementations§
Source§impl<S: Send + Sync> FromRequestParts<S> for SessionManager
impl<S: Send + Sync> FromRequestParts<S> for SessionManager
Auto Trait Implementations§
impl Freeze for SessionManager
impl !RefUnwindSafe for SessionManager
impl Send for SessionManager
impl Sync for SessionManager
impl Unpin for SessionManager
impl UnsafeUnpin for SessionManager
impl !UnwindSafe for SessionManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more