diff --git a/src/core/auth.rs b/src/core/auth.rs index cd59f75..cdef0b3 100644 --- a/src/core/auth.rs +++ b/src/core/auth.rs @@ -84,6 +84,7 @@ impl Auth { mongo_client: &Client, email: &str, password: &str, + user_agent: &str, ) -> Result { let user = match User::get_from_email(&mongo_client, email).await { Ok(user) => user, @@ -97,28 +98,10 @@ impl Auth { // verify the password if Password::verify_hash(password, &user.password) { - let session = match Session::new(&user, "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36").encrypt_add(&mongo_client, &dek_data.dek).await { + let session = match Session::new(&user, &user_agent).encrypt_add(&mongo_client, &dek_data.dek).await { Ok(session) => session, Err(e) => return Err(e), }; - // let res = Json(json!({ - // "message": "Signin successful", - // "user": { - // "uid": user.uid, - // "name": user.name, - // "email": user.email, - // "role": user.role, - // "created_at": user.created_at, - // "updated_at": user.updated_at, - // "email_verified": user.email_verified, - // "is_active": user.is_active, - // "session": { - // "session_id": Encryption::encrypt_data(&session.session_id, &dek_data.dek), - // "id_token" : session.id_token, - // "refresh_token" : session.refresh_token, - // }, - // }, - // })); let res = SignInOrSignUpResponse { message: "Signin successful".to_string(), diff --git a/src/handlers/auth_handler.rs b/src/handlers/auth_handler.rs index 4d058d4..36f604b 100644 --- a/src/handlers/auth_handler.rs +++ b/src/handlers/auth_handler.rs @@ -1,4 +1,4 @@ -use axum::{extract::State, Json}; +use axum::{extract::State, http::{header, HeaderMap}, Json}; use axum_macros::debug_handler; use crate::{ @@ -45,6 +45,7 @@ pub async fn signup_handler( pub async fn signin_handler( State(state): State, + header: HeaderMap, payload: Json, ) -> Result> { println!(">> HANDLER: signin_handler called"); @@ -55,7 +56,13 @@ pub async fn signin_handler( }); } - match Auth::sign_in(&state.mongo_client, &payload.email, &payload.password).await { + // get user-agent form the header + let user_agent = match header.get(header::USER_AGENT) { + Some(ua) => ua.to_str().unwrap().to_string(), + None => "".to_string(), + }; + + match Auth::sign_in(&state.mongo_client, &payload.email, &payload.password, &user_agent).await { Ok(res) => Ok(Json(res)), Err(e) => Err(e), }