Skip to main content

SessionStore

Trait SessionStore 

Source
pub trait SessionStore: Send + Sync {
    // Required methods
    fn create_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        user_id: &'life1 str,
        expires_at: u64,
    ) -> Pin<Box<dyn Future<Output = Result<TokenPair>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn get_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        refresh_token_hash: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<SessionData>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn revoke_session<'life0, 'life1, 'async_trait>(
        &'life0 self,
        refresh_token_hash: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn revoke_all_sessions<'life0, 'life1, 'async_trait>(
        &'life0 self,
        user_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

SessionStore trait - implement this for your storage backend

§Examples

Implement for PostgreSQL:

// Requires: sqlx PgPool and live PostgreSQL connection.
use async_trait::async_trait;
use fraiseql_auth::session::{SessionStore, SessionData, TokenPair};
use fraiseql_auth::error::Result;

pub struct PostgresSessionStore {
    // pool: sqlx::PgPool,
}

#[async_trait]
impl SessionStore for PostgresSessionStore {
    async fn create_session(&self, _user_id: &str, _expires_at: u64) -> Result<TokenPair> {
        panic!("example stub")
    }
    async fn get_session(&self, _refresh_token_hash: &str) -> Result<SessionData> {
        panic!("example stub")
    }
    async fn revoke_session(&self, _refresh_token_hash: &str) -> Result<()> {
        panic!("example stub")
    }
    async fn revoke_all_sessions(&self, _user_id: &str) -> Result<()> {
        panic!("example stub")
    }
}

Implement for Redis:

// Requires: redis crate and live Redis connection.
use async_trait::async_trait;
use fraiseql_auth::session::{SessionStore, SessionData, TokenPair};
use fraiseql_auth::error::Result;

pub struct RedisSessionStore {
    // client: redis::Client,
}

#[async_trait]
impl SessionStore for RedisSessionStore {
    async fn create_session(&self, _user_id: &str, _expires_at: u64) -> Result<TokenPair> {
        panic!("example stub")
    }
    async fn get_session(&self, _refresh_token_hash: &str) -> Result<SessionData> {
        panic!("example stub")
    }
    async fn revoke_session(&self, _refresh_token_hash: &str) -> Result<()> {
        panic!("example stub")
    }
    async fn revoke_all_sessions(&self, _user_id: &str) -> Result<()> {
        panic!("example stub")
    }
}

Required Methods§

Source

fn create_session<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, expires_at: u64, ) -> Pin<Box<dyn Future<Output = Result<TokenPair>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Create a new session and return token pair

§Arguments
  • user_id - The user identifier
  • expires_at - When the session should expire (Unix seconds)
§Returns

TokenPair with access_token and refresh_token

§Errors

Returns error if session creation fails

Source

fn get_session<'life0, 'life1, 'async_trait>( &'life0 self, refresh_token_hash: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<SessionData>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get session data by refresh token hash

§Arguments
  • refresh_token_hash - Hash of the refresh token
§Returns

SessionData if session exists and is not revoked

§Errors

Returns SessionError if session not found or revoked

Source

fn revoke_session<'life0, 'life1, 'async_trait>( &'life0 self, refresh_token_hash: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Revoke a single session

§Arguments
  • refresh_token_hash - Hash of the refresh token to revoke
§Errors

Returns error if revocation fails

Source

fn revoke_all_sessions<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Revoke all sessions for a user

§Arguments
  • user_id - The user identifier
§Errors

Returns error if revocation fails

Implementors§