Implement the core authentication traits
mainTo use axum-login, you must implement two primary traits for your user type and your backend:
AuthUser: Implemented on your user struct. It requires defining a uniqueIdtype and asession_auth_hash(used for session validation).AuthnBackend: Implemented on your backend struct. It defines how toauthenticatecredentials and how toget_uservia aUserId.
This allows you to use any user type and any backend (Database, LDAP, etc.).
use axum_login::{AuthUser, AuthnBackend, UserId};
#[derive(Clone, Debug)]
struct User;
impl AuthUser for User {
type Id = i64;
fn id(&self) -> Self::Id {
0
}
fn session_auth_hash(&self) -> &[u8] {
&[]
}
}
#[derive(Clone)]
struct Backend;
impl AuthnBackend for Backend {
type User = User;
type Credentials = ();
type Error = std::convert::Infallible;
async fn authenticate(
&self,
_: Self::Credentials,
) -> Result<Option<Self::User>, Self::Error> {
Ok(Some(User))
}
async fn get_user(
&self,
_: &UserId<Self>,
) -> Result<Option<Self::User>, Self::Error> {
Ok(Some(User))
}
}