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.
Each request receives its own SessionManager instance backed by its own
per-request state. There is no cross-request sharing; operations on one
request’s SessionManager cannot affect another request’s session state.
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 new session for user_id.
Any existing session is destroyed before the new one is created to prevent session-fixation attacks. The session cookie is set on the response automatically.
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 new session for user_id with custom JSON data attached.
Any existing session is destroyed before the new one is created to prevent session-fixation attacks. The session cookie is set on the response automatically.
Sourcepub async fn logout(&self) -> Result<(), Error>
pub async fn logout(&self) -> Result<(), Error>
Destroy the current session.
The session cookie is cleared on the response automatically. If there is no active session this is a no-op.
Sourcepub async fn logout_all(&self) -> Result<(), Error>
pub async fn logout_all(&self) -> Result<(), Error>
Destroy ALL sessions for the currently authenticated user.
The session cookie is cleared on the response automatically. If there is no active session this is a no-op.
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.
Returns Unauthorized if the request is not authenticated.
Sourcepub async fn revoke(&self, id: &SessionId) -> Result<(), Error>
pub async fn revoke(&self, id: &SessionId) -> Result<(), Error>
Destroy a specific session by ID.
Only sessions owned by the currently authenticated user can be revoked.
Returns Unauthorized if the request is not authenticated, or
NotFound if the target session does not exist or belongs to a
different user.
Sourcepub async fn rotate(&self) -> Result<(), Error>
pub async fn rotate(&self) -> Result<(), Error>
Regenerate the session token without changing the session ID or data.
The new token is set on the response cookie automatically.
Returns Unauthorized if the request is not authenticated.
Sourcepub async fn current(&self) -> Option<SessionData>
pub async fn current(&self) -> Option<SessionData>
Return the full session record for the current request, or None if
the request is not authenticated.
Sourcepub async fn user_id(&self) -> Option<String>
pub async fn user_id(&self) -> Option<String>
Return the authenticated user ID, or None if the request is not
authenticated.
Sourcepub async fn is_authenticated(&self) -> bool
pub async fn is_authenticated(&self) -> bool
Return true if the current request has an active, authenticated
session.
Sourcepub async fn list_my_sessions(&self) -> Result<Vec<SessionData>, Error>
pub async fn list_my_sessions(&self) -> Result<Vec<SessionData>, Error>
Return all active (non-expired) sessions for the authenticated user, ordered by most-recently-active first.
Returns Unauthorized if the request is not authenticated.
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>
Read a typed value from the session’s JSON data by key.
Returns Ok(None) if the key is absent or if the request is not
authenticated. Returns Ok(None) (with a tracing warning) if the
stored value cannot be deserialised into T.
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<T> DefaultHooks for T
impl<T> DefaultHooks for 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