Skip to content

Commit

Permalink
user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdip019 committed May 12, 2024
1 parent 0309e3c commit 0af9aee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
21 changes: 2 additions & 19 deletions src/core/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Auth {
mongo_client: &Client,
email: &str,
password: &str,
user_agent: &str,
) -> Result<SignInOrSignUpResponse> {
let user = match User::get_from_email(&mongo_client, email).await {
Ok(user) => user,
Expand All @@ -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(),
Expand Down
11 changes: 9 additions & 2 deletions src/handlers/auth_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::{extract::State, Json};
use axum::{extract::State, http::{header, HeaderMap}, Json};
use axum_macros::debug_handler;

use crate::{
Expand Down Expand Up @@ -45,6 +45,7 @@ pub async fn signup_handler(

pub async fn signin_handler(
State(state): State<AppState>,
header: HeaderMap,
payload: Json<SignInPayload>,
) -> Result<Json<SignInOrSignUpResponse>> {
println!(">> HANDLER: signin_handler called");
Expand All @@ -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),
}
Expand Down

0 comments on commit 0af9aee

Please sign in to comment.