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