pub struct SessionStore { /* private fields */ }Expand description
Low-level database-backed session store.
Handles all CRUD operations on the modo_sessions table. Application code
should rarely interact with SessionStore directly; use crate::SessionManager
(the axum extractor) for request-scoped session operations instead.
SessionStore is cheaply cloneable and is intended to be registered as a
managed service so it can be injected into background jobs.
Implementations§
Source§impl SessionStore
impl SessionStore
Sourcepub fn new(
db: &DbPool,
config: SessionConfig,
cookie_config: CookieConfig,
) -> Self
pub fn new( db: &DbPool, config: SessionConfig, cookie_config: CookieConfig, ) -> Self
Create a new store backed by db with the given session and cookie config.
Sourcepub fn config(&self) -> &SessionConfig
pub fn config(&self) -> &SessionConfig
Return a reference to the session configuration.
Return a reference to the cookie configuration.
Sourcepub async fn create(
&self,
meta: &SessionMeta,
user_id: &str,
data: Option<Value>,
) -> Result<(SessionData, SessionToken), Error>
pub async fn create( &self, meta: &SessionMeta, user_id: &str, data: Option<Value>, ) -> Result<(SessionData, SessionToken), Error>
Insert a new session for user_id and return the persisted SessionData
together with the plaintext SessionToken (to be set in the cookie).
After inserting, LRU eviction is applied if the user has exceeded
SessionConfig::max_sessions_per_user.
Sourcepub async fn read(&self, id: &SessionId) -> Result<Option<SessionData>, Error>
pub async fn read(&self, id: &SessionId) -> Result<Option<SessionData>, Error>
Load a session by its ID. Returns None if not found (does not check
expiry — call read_by_token for expiry-aware
lookup).
Sourcepub async fn read_by_token(
&self,
token: &SessionToken,
) -> Result<Option<SessionData>, Error>
pub async fn read_by_token( &self, token: &SessionToken, ) -> Result<Option<SessionData>, Error>
Load a non-expired session by plaintext token (hashes it internally).
Returns None if no matching, non-expired session is found.
Sourcepub async fn rotate_token(&self, id: &SessionId) -> Result<SessionToken, Error>
pub async fn rotate_token(&self, id: &SessionId) -> Result<SessionToken, Error>
Replace the token for a session with a newly generated one and return the new plaintext token. The session ID and all other fields are unchanged.
Sourcepub async fn touch(
&self,
id: &SessionId,
new_expires_at: DateTime<Utc>,
) -> Result<(), Error>
pub async fn touch( &self, id: &SessionId, new_expires_at: DateTime<Utc>, ) -> Result<(), Error>
Update last_active_at to now and set a new expires_at for a session.
Sourcepub async fn update_data(
&self,
id: &SessionId,
data: Value,
) -> Result<(), Error>
pub async fn update_data( &self, id: &SessionId, data: Value, ) -> Result<(), Error>
Replace the JSON payload stored in a session.
Sourcepub async fn destroy_all_for_user(&self, user_id: &str) -> Result<(), Error>
pub async fn destroy_all_for_user(&self, user_id: &str) -> Result<(), Error>
Delete all sessions belonging to user_id.
Sourcepub async fn destroy_all_except(
&self,
user_id: &str,
keep: &SessionId,
) -> Result<(), Error>
pub async fn destroy_all_except( &self, user_id: &str, keep: &SessionId, ) -> Result<(), Error>
Delete all sessions belonging to user_id except the one identified by
keep.
Sourcepub async fn list_for_user(
&self,
user_id: &str,
) -> Result<Vec<SessionData>, Error>
pub async fn list_for_user( &self, user_id: &str, ) -> Result<Vec<SessionData>, Error>
Return all non-expired sessions for user_id, ordered by most-recently-active
first.
Sourcepub async fn cleanup_expired(&self) -> Result<u64, Error>
pub async fn cleanup_expired(&self) -> Result<u64, Error>
Delete all sessions whose expires_at is in the past.
Returns the number of rows deleted. Called automatically by the
cleanup-job feature’s cron job.
Trait Implementations§
Source§impl Clone for SessionStore
impl Clone for SessionStore
Source§fn clone(&self) -> SessionStore
fn clone(&self) -> SessionStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SessionStore
impl !RefUnwindSafe for SessionStore
impl Send for SessionStore
impl Sync for SessionStore
impl Unpin for SessionStore
impl UnsafeUnpin for SessionStore
impl !UnwindSafe for SessionStore
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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