From f8347f0e0a4d4448483316d5c741eb95cdfde7ef Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Fri, 7 Jun 2024 15:30:16 +0100 Subject: [PATCH] signin, signout, signup --- src/account.rs | 70 ++++++++++++++++++++++++++++++++++++++--------- src/account/db.rs | 4 +++ src/lib.rs | 27 ++++++++++++++++-- 3 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/account.rs b/src/account.rs index 3c03a0e..f577d6e 100644 --- a/src/account.rs +++ b/src/account.rs @@ -1,15 +1,20 @@ pub mod db; use axum::{ - Router, - routing::{get, post} + extract::Path, + routing::{get, post}, + Json, + Router }; use axum_session_auth::AuthSession; use axum_session_mongo::SessionMongoPool; +use crate::AppError; use db::User; -use http::method::Method; -use mongodb::{Client, bson::oid::ObjectId}; +use mongodb::{ + bson::{doc, oid::ObjectId}, Client +}; +type AuthenticationSession = AuthSession; pub fn router() -> Router { Router::new() @@ -25,6 +30,8 @@ fn user_router() -> Router { get(get_sign_in) .post(post_sign_in) ) + .route("/sign-out", + post(post_sign_out)) .route("/backup", post(post_backup) ) @@ -33,15 +40,52 @@ fn user_router() -> Router { ) } -pub async fn get_sign_in() {} -pub async fn post_sign_in() {} +pub async fn get_sign_in() {} //TODO: Get Salt -pub async fn post_sign_up() {} +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? { + Some(_user) => { + auth.login_user(user_id); + auth.remember_user(true); + //TODO: Return API Key or Auth Key + }, + None => { + //TODO: Return or Redirect Unauthorized + }, + }; -pub async fn post_backup() {} - -pub async fn get_restore() {} - -async fn auth(method: Method, auth: AuthSession) { - //TODO: Auth loop (get from crate example) + Ok(String::new()) } + +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)?; + + match db.find_one(body, None).await? { + Some(user) => { + //TODO: Return or Redirect User Exists + }, + None => { + //TODO: Reutrn UsedId + } + } + Ok(()) +} + + +pub async fn post_sign_out(auth: AuthenticationSession) { + match auth.is_authenticated() { + true => auth.logout_user(), + false => { + //TODO: Redirect Not Logged in + }, + } +} + +pub async fn post_backup() {} //TODO: Backup + +pub async fn get_restore() {} //TODO: Restore diff --git a/src/account/db.rs b/src/account/db.rs index d9ff4e4..cab2d10 100644 --- a/src/account/db.rs +++ b/src/account/db.rs @@ -7,6 +7,10 @@ use mongodb::{ Client, }; +pub async fn get_db_client() -> Result { + Ok(Client::with_uri_str("mongodb://localhost:27017").await?) +} + #[derive(Serialize, Deserialize)] pub struct Mole { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] diff --git a/src/lib.rs b/src/lib.rs index 64d0220..3a14dd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,17 @@ mod account; mod model; -use account::db::User; +use account::db::{User, get_db_client}; use anyhow::Result; +use axum::response::{IntoResponse, Response}; use axum_session::{SessionConfig, SessionLayer, SessionStore}; use axum_session_auth::{AuthConfig, AuthSessionLayer}; use axum_session_mongo::SessionMongoPool; +use http::StatusCode; use mongodb::{bson::oid::ObjectId, Client}; pub async fn run() -> Result<()> { - let db = Client::with_uri_str("mongodb://localhost:27017").await?; + let db = get_db_client().await?; let session_store = session(db.clone()).await?; let auth_config = AuthConfig::::default(); @@ -39,3 +41,24 @@ async fn session(db: Client) -> Result> { Ok(SessionStore:: ::new(Some(db.clone().into()), session_config).await?) } + +struct AppError(anyhow::Error); + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", self.0), + ) + .into_response() + } +} + +impl From for AppError +where + E: Into, +{ + fn from(err: E) -> Self { + Self(err.into()) + } +}