2024-06-07 01:24:42 +00:00
|
|
|
pub mod db;
|
|
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
use axum::{
|
2024-06-07 14:30:16 +00:00
|
|
|
extract::Path,
|
|
|
|
|
routing::{get, post},
|
|
|
|
|
Json,
|
|
|
|
|
Router
|
2024-06-06 18:21:12 +00:00
|
|
|
};
|
2024-06-07 01:24:42 +00:00
|
|
|
use axum_session_auth::AuthSession;
|
|
|
|
|
use axum_session_mongo::SessionMongoPool;
|
2024-06-07 14:30:16 +00:00
|
|
|
use crate::AppError;
|
2024-06-07 01:24:42 +00:00
|
|
|
use db::User;
|
2024-06-07 14:30:16 +00:00
|
|
|
use mongodb::{
|
|
|
|
|
bson::{doc, oid::ObjectId}, Client
|
|
|
|
|
};
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
|
2024-06-06 19:43:32 +00:00
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
pub fn router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.nest("/:user_id", user_router())
|
|
|
|
|
.route("/",
|
|
|
|
|
post(post_sign_up)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn user_router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.route("/sign-in",
|
|
|
|
|
get(get_sign_in)
|
|
|
|
|
.post(post_sign_in)
|
|
|
|
|
)
|
2024-06-07 14:30:16 +00:00
|
|
|
.route("/sign-out",
|
|
|
|
|
post(post_sign_out))
|
2024-06-06 18:21:12 +00:00
|
|
|
.route("/backup",
|
|
|
|
|
post(post_backup)
|
|
|
|
|
)
|
|
|
|
|
.route("/restore",
|
|
|
|
|
get(get_restore)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
pub async fn get_sign_in() {} //TODO: Get Salt
|
|
|
|
|
|
|
|
|
|
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? {
|
|
|
|
|
Some(_user) => {
|
|
|
|
|
auth.login_user(user_id);
|
|
|
|
|
auth.remember_user(true);
|
|
|
|
|
//TODO: Return API Key or Auth Key
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
//TODO: Return or Redirect Unauthorized
|
|
|
|
|
},
|
|
|
|
|
};
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
Ok(String::new())
|
|
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
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)?;
|
|
|
|
|
|
|
|
|
|
match db.find_one(body, None).await? {
|
|
|
|
|
Some(user) => {
|
|
|
|
|
//TODO: Return or Redirect User Exists
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
//TODO: Reutrn UsedId
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 01:24:42 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
pub async fn post_sign_out(auth: AuthenticationSession) {
|
|
|
|
|
match auth.is_authenticated() {
|
|
|
|
|
true => auth.logout_user(),
|
|
|
|
|
false => {
|
|
|
|
|
//TODO: Redirect Not Logged in
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-06-07 01:24:42 +00:00
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
|
|
|
|
|
pub async fn post_backup() {} //TODO: Backup
|
|
|
|
|
|
|
|
|
|
pub async fn get_restore() {} //TODO: Restore
|