Trait mailpot_web::AuthUser
source · pub trait AuthUser<Id, Role = ()>: Clone + Send + Sync + 'staticwhere
Role: PartialOrd<Role> + PartialEq<Role> + Clone + Send + Sync + 'static,{
// Required methods
fn get_id(&self) -> Id;
fn get_password_hash(&self) -> Secret<Vec<u8, Global>>;
// Provided method
fn get_role(&self) -> Option<Role> { ... }
}
Expand description
A trait which defines methods that allow an arbitrary user type to be authenticated.
This trait must be implemented for arbitrary user types which wish to participate in the authentication process.
As an example, we might implement the trait for a custom user type,
MyUser
, like so:
use axum_login::AuthUser;
use secrecy::{ExposeSecret, SecretVec};
#[derive(Debug, Clone)]
struct MyUser {
id: i64,
name: String,
password_hash: String,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
enum Role {
User,
Admin,
}
impl AuthUser<i64, Role> for MyUser {
fn get_id(&self) -> i64 {
self.id
}
fn get_password_hash(&self) -> SecretVec<u8> {
SecretVec::new(self.password_hash.clone().into())
}
}
let user = MyUser {
id: 1,
name: "Ferris the Crab".to_string(),
password_hash: "hunter42".to_string(),
};
assert_eq!(user.get_id(), 1);
assert_eq!(
user.get_password_hash().expose_secret(),
SecretVec::new("hunter42".into()).expose_secret()
);