Skip to content

Commit

Permalink
refactor: move get_jwt function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsMani committed Nov 20, 2023
1 parent 3dc72f6 commit cd43bfe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
17 changes: 7 additions & 10 deletions crates/router/src/core/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::user as utils;
use api_models::user as api;
use diesel_models::enums::UserStatus;
use masking::{ExposeInterface, Secret};
use masking::ExposeInterface;

use super::errors::{UserErrors, UserResponse};
use crate::{
Expand All @@ -12,7 +13,7 @@ pub async fn signup(
request: api::SignUpRequest,
) -> UserResponse<api::SignUpResponse> {
let new_user = domain::NewUser::try_from(request)?;
let _ = new_user
new_user
.get_new_merchant()
.get_new_organization()
.insert_org_in_db(state.clone())
Expand All @@ -27,12 +28,10 @@ pub async fn signup(
UserStatus::Active,
)
.await?;
let jwt_token = user_from_db
.get_jwt_auth_token(state.clone(), user_role.org_id)
.await?;
let token = utils::generate_jwt_auth_token(state.clone(), &user_from_db, &user_role).await?;

return Ok(ApplicationResponse::Json(api::SignUpResponse {
token: Secret::new(jwt_token),
token,
merchant_id: user_role.merchant_id,
name: user_from_db.get_name(),
email: user_from_db.get_email(),
Expand Down Expand Up @@ -62,12 +61,10 @@ pub async fn signin(
user_from_db.compare_password(request.password)?;

let user_role = user_from_db.get_role_from_db(state.clone()).await?;
let jwt_token = user_from_db
.get_jwt_auth_token(state.clone(), user_role.org_id)
.await?;
let token = utils::generate_jwt_auth_token(state.clone(), &user_from_db, &user_role).await?;

return Ok(ApplicationResponse::Json(api::SignInResponse {
token: Secret::new(jwt_token),
token,
merchant_id: user_role.merchant_id,
name: user_from_db.get_name(),
email: user_from_db.get_email(),
Expand Down
19 changes: 0 additions & 19 deletions crates/router/src/types/domain/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::{
},
db::StorageInterface,
routes::AppState,
services::authentication::AuthToken,
types::transformers::ForeignFrom,
utils::user::password,
};
Expand Down Expand Up @@ -455,24 +454,6 @@ impl UserFromStorage {
self.0.email.clone()
}

pub async fn get_jwt_auth_token(&self, state: AppState, org_id: String) -> UserResult<String> {
let role_id = self.get_role_from_db(state.clone()).await?.role_id;
let merchant_id = state
.store
.find_user_role_by_user_id(self.get_user_id())
.await
.change_context(UserErrors::InternalServerError)?
.merchant_id;
AuthToken::new_token(
self.0.user_id.clone(),
merchant_id,
role_id,
&state.conf,
org_id,
)
.await
}

pub async fn get_role_from_db(&self, state: AppState) -> UserResult<UserRole> {
state
.store
Expand Down
31 changes: 31 additions & 0 deletions crates/router/src/utils/user.rs
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
use crate::{
core::errors::{UserResult, UserErrors}, routes::AppState, services::authentication::AuthToken,
types::domain::UserFromStorage,
};
use diesel_models::user_role::UserRole;
use error_stack::ResultExt;
use masking::Secret;

pub mod password;

pub async fn generate_jwt_auth_token(
state: AppState,
user: &UserFromStorage,
user_role: &UserRole,
) -> UserResult<Secret<String>> {
let role_id = user.get_role_from_db(state.clone()).await?.role_id;
let merchant_id = state
.store
.find_user_role_by_user_id(user.get_user_id())
.await
.change_context(UserErrors::InternalServerError)?
.merchant_id;
let token = AuthToken::new_token(
user.get_user_id().to_string(),
merchant_id,
role_id,
&state.conf,
user_role.org_id.to_owned(),
)
.await?;
Ok(Secret::new(token))
}

0 comments on commit cd43bfe

Please sign in to comment.