Skip to main content

Store

Struct Store 

Source
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

Source

pub fn new(db: Database, config: SessionConfig) -> Self

Create a store from a Database handle and session configuration.

Source

pub fn config(&self) -> &SessionConfig

Return the session configuration for this store.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn destroy(&self, id: &str) -> Result<()>

Delete a session by its ULID identifier.

§Errors

Returns an error if the database delete fails.

Source

pub async fn destroy_all_for_user(&self, user_id: &str) -> Result<()>

Delete all sessions belonging to a user.

§Errors

Returns an error if the database delete fails.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Store

Source§

fn clone(&self) -> Store

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Store

§

impl !RefUnwindSafe for Store

§

impl Send for Store

§

impl Sync for Store

§

impl Unpin for Store

§

impl UnsafeUnpin for Store

§

impl !UnwindSafe for Store

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more