From eedc536c23c3230c6ebeac8b495b2cfef0317830 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 23 Aug 2022 18:10:18 +0200 Subject: Auth login --- server/src/models/user.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'server/src/models/user.rs') diff --git a/server/src/models/user.rs b/server/src/models/user.rs index 964f04a..38ec121 100644 --- a/server/src/models/user.rs +++ b/server/src/models/user.rs @@ -16,7 +16,7 @@ pub struct User { #[derive(Deserialize, Serialize)] pub struct UserList { - id: i32, + pub id: i32, email: String, is_staff: Option, } @@ -61,6 +61,26 @@ impl User { Ok(rec) } + pub async fn find(user: User) -> Result { + let pool = unsafe { get_client() }; + + let crypted_password = sha256::digest(user.password); + + let rec = sqlx::query_as!( + UserList, + r#" + SELECT id, email, is_staff FROM "users" + WHERE email = $1 AND password = $2 + "#, + user.email, + crypted_password + ) + .fetch_one(pool) + .await?; + + Ok(rec) + } + pub async fn list() -> Result, AppError> { let pool = unsafe { get_client() }; let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#) -- cgit v1.2.3-18-g5258