UserProvider

Trait UserProvider 

Source
pub trait UserProvider:
    Send
    + Sync
    + 'static {
    // Required method
    fn retrieve_by_id<'life0, 'async_trait>(
        &'life0 self,
        id: i64,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Arc<dyn Authenticatable>>, FrameworkError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn retrieve_by_credentials<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _credentials: &'life1 Value,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Arc<dyn Authenticatable>>, FrameworkError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn validate_credentials<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        _user: &'life1 dyn Authenticatable,
        _credentials: &'life2 Value,
    ) -> Pin<Box<dyn Future<Output = Result<bool, FrameworkError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

Trait for retrieving authenticated users from storage

The application must implement this trait and register it with the container to enable Auth::user().

§Example

use kit::auth::{UserProvider, Authenticatable};
use kit::FrameworkError;
use async_trait::async_trait;
use std::sync::Arc;

pub struct DatabaseUserProvider;

#[async_trait]
impl UserProvider for DatabaseUserProvider {
    async fn retrieve_by_id(&self, id: i64) -> Result<Option<Arc<dyn Authenticatable>>, FrameworkError> {
        let user = User::query()
            .filter(Column::Id.eq(id as i32))
            .first()
            .await?;
        Ok(user.map(|u| Arc::new(u) as Arc<dyn Authenticatable>))
    }
}

Required Methods§

Source

fn retrieve_by_id<'life0, 'async_trait>( &'life0 self, id: i64, ) -> Pin<Box<dyn Future<Output = Result<Option<Arc<dyn Authenticatable>>, FrameworkError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Retrieve a user by their unique identifier

Provided Methods§

Source

fn retrieve_by_credentials<'life0, 'life1, 'async_trait>( &'life0 self, _credentials: &'life1 Value, ) -> Pin<Box<dyn Future<Output = Result<Option<Arc<dyn Authenticatable>>, FrameworkError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieve a user by credentials (for custom authentication flows)

Default implementation returns None (not supported). Override this if you need to authenticate by credentials other than ID.

Source

fn validate_credentials<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _user: &'life1 dyn Authenticatable, _credentials: &'life2 Value, ) -> Pin<Box<dyn Future<Output = Result<bool, FrameworkError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Validate credentials against a user

Default implementation returns false (not supported). Override this if you need password validation.

Implementors§