pub struct Store { /* private fields */ }Expand description
Low-level SQLite-backed session store.
Wraps a Database handle and exposes async methods for all session CRUD
operations. Consumed by super::middleware::SessionLayer and available
to handlers via super::extractor::Session.
Implementations§
Source§impl Store
impl Store
Sourcepub fn new(db: Database, config: SessionConfig) -> Self
pub fn new(db: Database, config: SessionConfig) -> Self
Create a store from a Database handle and session configuration.
Sourcepub fn config(&self) -> &SessionConfig
pub fn config(&self) -> &SessionConfig
Return the session configuration for this store.
Sourcepub async fn read_by_token(
&self,
token: &SessionToken,
) -> Result<Option<SessionData>>
pub async fn read_by_token( &self, token: &SessionToken, ) -> Result<Option<SessionData>>
Look up an active (non-expired) session by its token hash.
Returns None if no matching session exists or the session has expired.
§Errors
Returns an error if the database query fails or the stored data cannot be deserialised.
Sourcepub async fn read(&self, id: &str) -> Result<Option<SessionData>>
pub async fn read(&self, id: &str) -> Result<Option<SessionData>>
Look up a session by its ULID identifier (ignores expiry).
Returns None if no session with that ID exists.
§Errors
Returns an error if the database query fails or the stored data cannot be deserialised.
Sourcepub async fn list_for_user(&self, user_id: &str) -> Result<Vec<SessionData>>
pub async fn list_for_user(&self, user_id: &str) -> Result<Vec<SessionData>>
List all active (non-expired) sessions for a user, ordered by most recently active.
§Errors
Returns an error if the database query fails or the stored data cannot be deserialised.
Sourcepub async fn create(
&self,
meta: &SessionMeta,
user_id: &str,
data: Option<Value>,
) -> Result<(SessionData, SessionToken)>
pub async fn create( &self, meta: &SessionMeta, user_id: &str, data: Option<Value>, ) -> Result<(SessionData, SessionToken)>
Create a new session for the given user.
Inserts the session row then trims excess sessions when the
max_sessions_per_user limit is exceeded by evicting the oldest
session(s).
Returns the newly-created SessionData and the raw SessionToken that
must be placed in the cookie.
§Errors
Returns an error if the session data cannot be serialised or the database insert/eviction query fails.
Sourcepub async fn destroy_all_for_user(&self, user_id: &str) -> Result<()>
pub async fn destroy_all_for_user(&self, user_id: &str) -> Result<()>
Sourcepub async fn destroy_all_except(
&self,
user_id: &str,
keep_id: &str,
) -> Result<()>
pub async fn destroy_all_except( &self, user_id: &str, keep_id: &str, ) -> Result<()>
Delete all sessions for a user except the one with the given ID.
Used to implement “log out other devices”.
§Errors
Returns an error if the database delete fails.
Sourcepub async fn rotate_token(&self, id: &str) -> Result<SessionToken>
pub async fn rotate_token(&self, id: &str) -> Result<SessionToken>
Issue a new token for an existing session, invalidating the old one.
Returns the new SessionToken. The middleware will write this token
to the session cookie on the response.
§Errors
Returns an error if the database update fails.
Sourcepub async fn flush(
&self,
id: &str,
data: &Value,
now: DateTime<Utc>,
expires_at: DateTime<Utc>,
) -> Result<()>
pub async fn flush( &self, id: &str, data: &Value, now: DateTime<Utc>, expires_at: DateTime<Utc>, ) -> Result<()>
Persist the session’s JSON data and update last_active_at / expires_at.
Called by the middleware at the end of a request when the session was marked dirty.
§Errors
Returns an error if the session data cannot be serialised or the database update fails.
Sourcepub async fn touch(
&self,
id: &str,
now: DateTime<Utc>,
expires_at: DateTime<Utc>,
) -> Result<()>
pub async fn touch( &self, id: &str, now: DateTime<Utc>, expires_at: DateTime<Utc>, ) -> Result<()>
Update last_active_at and expires_at without changing session data.
Called by the middleware when the touch interval has elapsed but the session data is not dirty.
§Errors
Returns an error if the database update fails.
Sourcepub async fn cleanup_expired(&self) -> Result<u64>
pub async fn cleanup_expired(&self) -> Result<u64>
Delete all sessions whose expires_at is in the past.
Returns the number of rows deleted. Schedule this periodically (e.g. via a cron job) to keep the table small.
§Errors
Returns an error if the database delete fails.