From ea16071903a7706f28ee5d108dc19f7237d51d01 Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Fri, 7 Jun 2024 16:37:00 +0100 Subject: [PATCH] get salt --- src/account.rs | 40 +++++++++++++++++++++++++++------------- src/account/db.rs | 10 +++++++++- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/account.rs b/src/account.rs index f577d6e..308a9df 100644 --- a/src/account.rs +++ b/src/account.rs @@ -9,9 +9,10 @@ use axum::{ use axum_session_auth::AuthSession; use axum_session_mongo::SessionMongoPool; use crate::AppError; -use db::User; +use db::{get_users, User}; use mongodb::{ - bson::{doc, oid::ObjectId}, Client + bson::{doc, oid::ObjectId, to_document}, + Client }; type AuthenticationSession = AuthSession; @@ -40,17 +41,31 @@ fn user_router() -> Router { ) } -pub async fn get_sign_in() {} //TODO: Get Salt +pub async fn get_sign_in(Path(user_id): Path) -> Result { + let db = get_users().await?; + let query = doc! { "_id" : user_id}; + + match db.find_one(query, None).await? { + Some(user) => { + //TODO: Return User salt + Ok(String::new()) + }, + None => { + //TODO: Return User does not exist + Ok(String::new()) + } + } +} pub async fn post_sign_in(Path(user_id): Path, auth: AuthenticationSession, body: String) -> Result { - let db = db::get_db_client().await? - .database("dermy").collection::("users"); - - match db.find_one(doc!{ "_id": &user_id, "_auth._hash": body}, None).await? { + let db = get_users().await?; + let query = doc! { "_id": &user_id, "_auth._hash": body }; + + match db.find_one(query, None).await? { Some(_user) => { auth.login_user(user_id); auth.remember_user(true); - //TODO: Return API Key or Auth Key + //TODO: Return API Key or Auth Key and User ID }, None => { //TODO: Return or Redirect Unauthorized @@ -61,12 +76,11 @@ pub async fn post_sign_in(Path(user_id): Path, auth: AuthenticationSes } pub async fn post_sign_up(Json(body): Json) -> Result<(), AppError> { - let db = db::get_db_client().await? - .database("dermy").collection::("users"); - let body = mongodb::bson::to_document(&body)?; + let db = get_users().await?; + let query = to_document(&body)?; - match db.find_one(body, None).await? { - Some(user) => { + match db.find_one(query, None).await? { + Some(_user) => { //TODO: Return or Redirect User Exists }, None => { diff --git a/src/account/db.rs b/src/account/db.rs index cab2d10..32e214a 100644 --- a/src/account/db.rs +++ b/src/account/db.rs @@ -4,13 +4,21 @@ use axum_session_auth::Authentication; use serde::{Deserialize, Serialize}; use mongodb::{ bson::{doc, oid::ObjectId, DateTime}, - Client, + Client, Collection, Database, }; pub async fn get_db_client() -> Result { Ok(Client::with_uri_str("mongodb://localhost:27017").await?) } +pub async fn get_database() -> Result { + Ok(get_db_client().await?.database("dermy")) +} + +pub async fn get_users() -> Result> { + Ok(get_database().await?.collection::("users")) +} + #[derive(Serialize, Deserialize)] pub struct Mole { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]