Move auth_backend to util
This commit is contained in:
42
src/util/auth_backend.rs
Normal file
42
src/util/auth_backend.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use axum_login::{AuthnBackend, AuthUser, UserId};
|
||||
use crate::api::users::UserCredentials;
|
||||
use leptos::server_fn::error::ServerFnErrorErr;
|
||||
|
||||
use crate::models::backend::User;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
|
||||
impl AuthUser for User {
|
||||
type Id = i32;
|
||||
|
||||
// TODO: Ideally, we shouldn't have to unwrap here
|
||||
|
||||
fn id(&self) -> Self::Id {
|
||||
self.id.unwrap()
|
||||
}
|
||||
|
||||
fn session_auth_hash(&self) -> &[u8] {
|
||||
self.password.as_ref().unwrap().as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AuthBackend;
|
||||
|
||||
#[async_trait]
|
||||
impl AuthnBackend for AuthBackend {
|
||||
type User = User;
|
||||
type Credentials = UserCredentials;
|
||||
type Error = ServerFnErrorErr;
|
||||
|
||||
async fn authenticate(&self, creds: Self::Credentials) -> Result<Option<Self::User>, Self::Error> {
|
||||
crate::api::users::validate_user(creds).await
|
||||
.map_err(|e| ServerFnErrorErr::ServerError(format!("Error validating user: {}", e)))
|
||||
}
|
||||
|
||||
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
|
||||
crate::api::users::find_user_by_id(*user_id).await
|
||||
.map_err(|e| ServerFnErrorErr::ServerError(format!("Error getting user: {}", e)))
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ cfg_if! {
|
||||
pub mod require_auth;
|
||||
pub mod fileserv;
|
||||
pub mod database;
|
||||
pub mod auth_backend;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ use axum::middleware::Next;
|
||||
use axum_login::AuthSession;
|
||||
use http::StatusCode;
|
||||
|
||||
use crate::auth_backend::AuthBackend;
|
||||
use crate::util::auth_backend::AuthBackend;
|
||||
|
||||
use axum::extract::FromRequestParts;
|
||||
|
||||
|
Reference in New Issue
Block a user