This commit is contained in:
Joshua Perry 2024-06-07 16:37:00 +01:00
parent f8347f0e0a
commit ea16071903
2 changed files with 36 additions and 14 deletions

View File

@ -9,9 +9,10 @@ use axum::{
use axum_session_auth::AuthSession; use axum_session_auth::AuthSession;
use axum_session_mongo::SessionMongoPool; use axum_session_mongo::SessionMongoPool;
use crate::AppError; use crate::AppError;
use db::User; use db::{get_users, User};
use mongodb::{ use mongodb::{
bson::{doc, oid::ObjectId}, Client bson::{doc, oid::ObjectId, to_document},
Client
}; };
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>; type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
@ -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<ObjectId>) -> Result<String, AppError> {
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<ObjectId>, auth: AuthenticationSession, body: String) -> Result<String, AppError> { pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSession, body: String) -> Result<String, AppError> {
let db = db::get_db_client().await? let db = get_users().await?;
.database("dermy").collection::<User>("users"); let query = doc! { "_id": &user_id, "_auth._hash": body };
match db.find_one(doc!{ "_id": &user_id, "_auth._hash": body}, None).await? { match db.find_one(query, None).await? {
Some(_user) => { Some(_user) => {
auth.login_user(user_id); auth.login_user(user_id);
auth.remember_user(true); auth.remember_user(true);
//TODO: Return API Key or Auth Key //TODO: Return API Key or Auth Key and User ID
}, },
None => { None => {
//TODO: Return or Redirect Unauthorized //TODO: Return or Redirect Unauthorized
@ -61,12 +76,11 @@ pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSes
} }
pub async fn post_sign_up(Json(body): Json<User>) -> Result<(), AppError> { pub async fn post_sign_up(Json(body): Json<User>) -> Result<(), AppError> {
let db = db::get_db_client().await? let db = get_users().await?;
.database("dermy").collection::<User>("users"); let query = to_document(&body)?;
let body = mongodb::bson::to_document(&body)?;
match db.find_one(body, None).await? { match db.find_one(query, None).await? {
Some(user) => { Some(_user) => {
//TODO: Return or Redirect User Exists //TODO: Return or Redirect User Exists
}, },
None => { None => {

View File

@ -4,13 +4,21 @@ use axum_session_auth::Authentication;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use mongodb::{ use mongodb::{
bson::{doc, oid::ObjectId, DateTime}, bson::{doc, oid::ObjectId, DateTime},
Client, Client, Collection, Database,
}; };
pub async fn get_db_client() -> Result<Client> { pub async fn get_db_client() -> Result<Client> {
Ok(Client::with_uri_str("mongodb://localhost:27017").await?) Ok(Client::with_uri_str("mongodb://localhost:27017").await?)
} }
pub async fn get_database() -> Result<Database> {
Ok(get_db_client().await?.database("dermy"))
}
pub async fn get_users() -> Result<Collection<User>> {
Ok(get_database().await?.collection::<User>("users"))
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Mole { pub struct Mole {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")] #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]