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_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<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> {
let db = db::get_db_client().await?
.database("dermy").collection::<User>("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<ObjectId>, auth: AuthenticationSes
}
pub async fn post_sign_up(Json(body): Json<User>) -> Result<(), AppError> {
let db = db::get_db_client().await?
.database("dermy").collection::<User>("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 => {

View File

@ -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<Client> {
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)]
pub struct Mole {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]