Files
LibreTunes/src/util/auth_backend.rs
Ethan Girouard 297c22d832
Some checks failed
Push Workflows / docs (push) Successful in 1m35s
Push Workflows / rustfmt (push) Successful in 11s
Push Workflows / clippy (push) Failing after 58s
Push Workflows / leptos-test (push) Successful in 3m4s
Push Workflows / test (push) Successful in 3m22s
Push Workflows / build (push) Successful in 4m43s
Push Workflows / docker-build (push) Failing after 14m42s
Push Workflows / nix-build (push) Successful in 17m22s
Run rustfmt
2025-04-29 22:27:24 +00:00

47 lines
1.2 KiB
Rust

use crate::api::users::UserCredentials;
use axum_login::{AuthUser, AuthnBackend, UserId};
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}")))
}
}