-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(users): Separate signup and signin #2921
Merged
+453
−109
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
30be980
feat: seperate signup and signin
ThisIsMani 3ceccf9
Merge branch 'main' into signup-signin
ThisIsMani a03cb5f
Merge branch 'main' into signup-signin
ThisIsMani 3dc72f6
Merge branch 'main' of https://github.com/juspay/hyperswitch into sig…
ThisIsMani cd43bfe
refactor: move get_jwt function to utils
ThisIsMani b9e8467
Merge branch 'main' of https://github.com/juspay/hyperswitch into sig…
ThisIsMani 26d0732
Merge branch 'main' of https://github.com/juspay/hyperswitch into sig…
ThisIsMani f409bea
feat: add signup apis
ThisIsMani ff168a1
Merge branch 'main' of https://github.com/juspay/hyperswitch into sig…
ThisIsMani 4ba6d65
chore: run formatter
hyperswitch-bot[bot] e7137a9
refactor: remove email from default features
ThisIsMani 48a9393
refactor: add email cfg flags
ThisIsMani 996c3dd
refactor: use merchant_id from user_role instead of request in switch…
ThisIsMani ed0b617
refactor: make get_dashboard_entry_response function take onwership o…
ThisIsMani a4e1afd
Merge branch 'main' into signup-signin
ThisIsMani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,78 @@ | ||
use api_models::user as api; | ||
use diesel_models::enums::UserStatus; | ||
use error_stack::IntoReport; | ||
use masking::{ExposeInterface, Secret}; | ||
use router_env::env; | ||
|
||
use super::errors::{UserErrors, UserResponse}; | ||
use crate::{ | ||
consts::user as consts, routes::AppState, services::ApplicationResponse, types::domain, | ||
}; | ||
|
||
pub async fn connect_account( | ||
pub async fn signup( | ||
state: AppState, | ||
request: api::ConnectAccountRequest, | ||
) -> UserResponse<api::ConnectAccountResponse> { | ||
let find_user = state | ||
.store | ||
.find_user_by_email(request.email.clone().expose().expose().as_str()) | ||
.await; | ||
request: api::SignUpRequest, | ||
) -> UserResponse<api::SignUpResponse> { | ||
let new_user = domain::NewUser::try_from(request)?; | ||
let _ = new_user | ||
.get_new_merchant() | ||
.get_new_organization() | ||
.insert_org_in_db(state.clone()) | ||
.await?; | ||
let user_from_db = new_user | ||
.insert_user_and_merchant_in_db(state.clone()) | ||
.await?; | ||
let user_role = new_user | ||
.insert_user_role_in_db( | ||
state.clone(), | ||
consts::ROLE_ID_ORGANIZATION_ADMIN.to_string(), | ||
UserStatus::Active, | ||
) | ||
.await?; | ||
let jwt_token = user_from_db | ||
.get_jwt_auth_token(state.clone(), user_role.org_id) | ||
.await?; | ||
|
||
if let Ok(found_user) = find_user { | ||
let user_from_db: domain::UserFromStorage = found_user.into(); | ||
|
||
user_from_db.compare_password(request.password)?; | ||
return Ok(ApplicationResponse::Json(api::SignUpResponse { | ||
token: Secret::new(jwt_token), | ||
merchant_id: user_role.merchant_id, | ||
name: user_from_db.get_name(), | ||
email: user_from_db.get_email(), | ||
verification_days_left: None, | ||
user_role: user_role.role_id, | ||
user_id: user_from_db.get_user_id().to_string(), | ||
})); | ||
} | ||
|
||
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?; | ||
pub async fn signin( | ||
state: AppState, | ||
request: api::SignInRequest, | ||
) -> UserResponse<api::SignInResponse> { | ||
let user_from_db: domain::UserFromStorage = state | ||
.store | ||
.find_user_by_email(request.email.clone().expose().expose().as_str()) | ||
.await | ||
.map_err(|e| { | ||
if e.current_context().is_db_not_found() { | ||
e.change_context(UserErrors::InvalidCredentials) | ||
} else { | ||
e.change_context(UserErrors::InternalServerError) | ||
} | ||
})? | ||
.into(); | ||
|
||
return Ok(ApplicationResponse::Json(api::ConnectAccountResponse { | ||
token: Secret::new(jwt_token), | ||
merchant_id: user_role.merchant_id, | ||
name: user_from_db.get_name(), | ||
email: user_from_db.get_email(), | ||
verification_days_left: None, | ||
user_role: user_role.role_id, | ||
user_id: user_from_db.get_user_id().to_string(), | ||
})); | ||
} else if find_user | ||
.map_err(|e| e.current_context().is_db_not_found()) | ||
.err() | ||
.unwrap_or(false) | ||
{ | ||
if matches!(env::which(), env::Env::Production) { | ||
return Err(UserErrors::InvalidCredentials).into_report(); | ||
} | ||
user_from_db.compare_password(request.password)?; | ||
|
||
let new_user = domain::NewUser::try_from(request)?; | ||
let _ = new_user | ||
.get_new_merchant() | ||
.get_new_organization() | ||
.insert_org_in_db(state.clone()) | ||
.await?; | ||
let user_from_db = new_user | ||
.insert_user_and_merchant_in_db(state.clone()) | ||
.await?; | ||
let user_role = new_user | ||
.insert_user_role_in_db( | ||
state.clone(), | ||
consts::ROLE_ID_ORGANIZATION_ADMIN.to_string(), | ||
UserStatus::Active, | ||
) | ||
.await?; | ||
let jwt_token = user_from_db | ||
.get_jwt_auth_token(state.clone(), user_role.org_id) | ||
.await?; | ||
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?; | ||
|
||
return Ok(ApplicationResponse::Json(api::ConnectAccountResponse { | ||
token: Secret::new(jwt_token), | ||
merchant_id: user_role.merchant_id, | ||
name: user_from_db.get_name(), | ||
email: user_from_db.get_email(), | ||
verification_days_left: None, | ||
user_role: user_role.role_id, | ||
user_id: user_from_db.get_user_id().to_string(), | ||
})); | ||
} else { | ||
Err(UserErrors::InternalServerError.into()) | ||
} | ||
return Ok(ApplicationResponse::Json(api::SignInResponse { | ||
token: Secret::new(jwt_token), | ||
merchant_id: user_role.merchant_id, | ||
name: user_from_db.get_name(), | ||
email: user_from_db.get_email(), | ||
verification_days_left: None, | ||
user_role: user_role.role_id, | ||
user_id: user_from_db.get_user_id().to_string(), | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
combine with above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can't be combined because the above function doesn't return
UserFromStorage
.