AuthBackend

Trait AuthBackend 

Source
pub trait AuthBackend: Send + Sync {
    // Required methods
    fn authenticate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        credentials: &'life1 (dyn Any + Send + Sync),
    ) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User + Send + Sync>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn get_by_id<'life0, 'async_trait>(
        &'life0 self,
        id: UserId,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User + Send + Sync>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

An authentication backend.

Required Methods§

Source

fn authenticate<'life0, 'life1, 'async_trait>( &'life0 self, credentials: &'life1 (dyn Any + Send + Sync), ) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User + Send + Sync>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Authenticates a user with the given credentials.

This method returns a user object if the authentication is successful. If the authentication fails, it returns None.

§Errors

Returns an error if the user object cannot be fetched.

Returns an error if the credentials type is not supported.

Source

fn get_by_id<'life0, 'async_trait>( &'life0 self, id: UserId, ) -> Pin<Box<dyn Future<Output = Result<Option<Box<dyn User + Send + Sync>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get a user by ID.

This method returns a user object by its ID. If the user is not found, it should return None.

§Errors

Returns an error if the user object cannot be fetched.

Returns an error if the user ID type is not supported.

§Examples
use cot::auth::UserId;
use cot::request::{Request, RequestExt};

async fn view_user_profile(request: &Request) {
    let user = request
        .context()
        .auth_backend()
        .get_by_id(UserId::Int(1))
        .await;

    match user {
        Ok(Some(user)) => {
            println!("User ID: {:?}", user.id());
            println!("Username: {:?}", user.username());
        }
        Ok(None) => {
            println!("User not found");
        }
        Err(error) => {
            eprintln!("Error: {}", error);
        }
    }
}

Implementors§

Source§

impl AuthBackend for DatabaseUserBackend

Available on crate feature db only.
Source§

impl AuthBackend for NoAuthBackend