From 5c0286727384f2ac2e0ac764186a4dd371bea940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Tue, 18 Jun 2024 21:57:17 +0200 Subject: [PATCH] feat: implementing slowly user with sea_orm --- rear_auth/src/users.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rear_auth/src/users.rs b/rear_auth/src/users.rs index 9a5f853..994df33 100644 --- a/rear_auth/src/users.rs +++ b/rear_auth/src/users.rs @@ -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, Self::Error> { - let user: Option = 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(|| {