signin, signout, signup
This commit is contained in:
parent
56ef6e3df9
commit
f8347f0e0a
|
|
@ -1,15 +1,20 @@
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
extract::Path,
|
||||||
routing::{get, post}
|
routing::{get, post},
|
||||||
|
Json,
|
||||||
|
Router
|
||||||
};
|
};
|
||||||
use axum_session_auth::AuthSession;
|
use axum_session_auth::AuthSession;
|
||||||
use axum_session_mongo::SessionMongoPool;
|
use axum_session_mongo::SessionMongoPool;
|
||||||
|
use crate::AppError;
|
||||||
use db::User;
|
use db::User;
|
||||||
use http::method::Method;
|
use mongodb::{
|
||||||
use mongodb::{Client, bson::oid::ObjectId};
|
bson::{doc, oid::ObjectId}, Client
|
||||||
|
};
|
||||||
|
|
||||||
|
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
|
||||||
|
|
||||||
pub fn router() -> Router {
|
pub fn router() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
|
|
@ -25,6 +30,8 @@ fn user_router() -> Router {
|
||||||
get(get_sign_in)
|
get(get_sign_in)
|
||||||
.post(post_sign_in)
|
.post(post_sign_in)
|
||||||
)
|
)
|
||||||
|
.route("/sign-out",
|
||||||
|
post(post_sign_out))
|
||||||
.route("/backup",
|
.route("/backup",
|
||||||
post(post_backup)
|
post(post_backup)
|
||||||
)
|
)
|
||||||
|
|
@ -33,15 +40,52 @@ fn user_router() -> Router {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_sign_in() {}
|
pub async fn get_sign_in() {} //TODO: Get Salt
|
||||||
pub async fn post_sign_in() {}
|
|
||||||
|
|
||||||
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");
|
||||||
|
|
||||||
|
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() {}
|
Ok(String::new())
|
||||||
|
|
||||||
pub async fn get_restore() {}
|
|
||||||
|
|
||||||
async fn auth(method: Method, auth: AuthSession<User, ObjectId, SessionMongoPool, Client>) {
|
|
||||||
//TODO: Auth loop (get from crate example)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
Client,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub async fn get_db_client() -> Result<Client> {
|
||||||
|
Ok(Client::with_uri_str("mongodb://localhost:27017").await?)
|
||||||
|
}
|
||||||
|
|
||||||
#[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")]
|
||||||
|
|
|
||||||
27
src/lib.rs
27
src/lib.rs
|
|
@ -1,15 +1,17 @@
|
||||||
mod account;
|
mod account;
|
||||||
mod model;
|
mod model;
|
||||||
|
|
||||||
use account::db::User;
|
use account::db::{User, get_db_client};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use axum::response::{IntoResponse, Response};
|
||||||
use axum_session::{SessionConfig, SessionLayer, SessionStore};
|
use axum_session::{SessionConfig, SessionLayer, SessionStore};
|
||||||
use axum_session_auth::{AuthConfig, AuthSessionLayer};
|
use axum_session_auth::{AuthConfig, AuthSessionLayer};
|
||||||
use axum_session_mongo::SessionMongoPool;
|
use axum_session_mongo::SessionMongoPool;
|
||||||
|
use http::StatusCode;
|
||||||
use mongodb::{bson::oid::ObjectId, Client};
|
use mongodb::{bson::oid::ObjectId, Client};
|
||||||
|
|
||||||
pub async fn run() -> Result<()> {
|
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 session_store = session(db.clone()).await?;
|
||||||
let auth_config = AuthConfig::<ObjectId>::default();
|
let auth_config = AuthConfig::<ObjectId>::default();
|
||||||
|
|
||||||
|
|
@ -39,3 +41,24 @@ async fn session(db: Client) -> Result<SessionStore<SessionMongoPool>> {
|
||||||
Ok(SessionStore::<SessionMongoPool>
|
Ok(SessionStore::<SessionMongoPool>
|
||||||
::new(Some(db.clone().into()), session_config).await?)
|
::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