feat: implementing slowly user with sea_orm

This commit is contained in:
Gabor Körber 2024-06-18 21:57:17 +02:00
parent 84c127edcd
commit 5c02867273

View File

@ -1,13 +1,13 @@
use async_trait::async_trait;
use axum_login::{AuthUser, AuthnBackend, UserId};
use password_auth::verify_password;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, ModelTrait, Set};
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::task;
#[derive(Debug, Clone)]
struct UserDatabase {
pub struct UserDatabase {
connection: DatabaseConnection,
}
@ -113,11 +113,18 @@ impl AuthnBackend for UserDatabase {
&self,
creds: Self::Credentials,
) -> Result<Option<Self::User>, Self::Error> {
let user: Option<Self::User> = sqlx::query_as("select * from users where username = ? ")
.bind(creds.username)
.fetch_optional(&self.db)
let user_found = entity::User::find()
.filter(entity::user::Column::Username.eq(creds.username))
.one(&self.connection)
.await?;
let user = if let Some(user) = user_found {
let rear_user: User = user.into();
Some(rear_user)
} else {
None
};
// Verifying the password is blocking and potentially slow, so we'll do so via
// `spawn_blocking`.
task::spawn_blocking(|| {