signin, signout, signup
This commit is contained in:
parent
56ef6e3df9
commit
f8347f0e0a
|
|
@ -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<User, ObjectId, SessionMongoPool, Client>;
|
||||
|
||||
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<ObjectId>, auth: AuthenticationSession, body: String) -> Result<String, AppError> {
|
||||
let db = db::get_db_client().await?
|
||||
.database("dermy").collection::<User>("users");
|
||||
|
||||
pub async fn post_backup() {}
|
||||
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 get_restore() {}
|
||||
|
||||
async fn auth(method: Method, auth: AuthSession<User, ObjectId, SessionMongoPool, Client>) {
|
||||
//TODO: Auth loop (get from crate example)
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ use mongodb::{
|
|||
Client,
|
||||
};
|
||||
|
||||
pub async fn get_db_client() -> Result<Client> {
|
||||
Ok(Client::with_uri_str("mongodb://localhost:27017").await?)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Mole {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
|
|
|
|||
27
src/lib.rs
27
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::<ObjectId>::default();
|
||||
|
||||
|
|
@ -39,3 +41,24 @@ async fn session(db: Client) -> Result<SessionStore<SessionMongoPool>> {
|
|||
Ok(SessionStore::<SessionMongoPool>
|
||||
::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<E> From<E> for AppError
|
||||
where
|
||||
E: Into<anyhow::Error>,
|
||||
{
|
||||
fn from(err: E) -> Self {
|
||||
Self(err.into())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue