AuthController Validation and Exceptions Flow #49
Dynavy
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
AuthController Validation Flow:
The AuthController handles both user login and user registration endpoints requests. When a request is received, the flow follows these steps:
Receive Request:
The system can receive two types of requests at the
AuthController
endpoint: user login or user registration.For the loginUser() method, the request will contain a UserLoginRequest DTO, typically including the user's
email
andpassword
.For the registerUser() method, the request will contain a UserRegisterRequest DTO, which includes user data such as
name
,email
,password
, and optionallyphoneNumber
.DTO Validation:
UserLoginRequest DTO:
email
andpassword
fields are validated with annotations such as@NotBlank
(ensuring that they are not empty) and@Size
(ensuring the fields meet the required size, like the password being of a minimum length).UserRegisterRequest DTO:
name
,email
,password
, andphoneNumber
fields undergo validation:@NotBlank
ensures thename
andemail
are not empty.@Size
ensures thepassword
meets the required length.Custom validations are applied to ensure the
email
format is correct and that thephoneNumber
(if provided) is valid.The ValidationService handles both the UserLoginRequest and UserRegisterRequest validations. If any validation fails, a ValidationErrorResponse DTO is returned with detailed error messages, indicating which fields failed the validation.
Validation Outcome:
If the validation is successful:
For UserLoginRequest, the flow continues to the AuthService to authenticate the user.
For UserRegisterRequest, the flow continues to the UserService to check if the user already exists in the system.
If the validation fails, a ValidationErrorResponse DTO is returned, which provides information about the failed validation.
Business Logic and Custom Validations in AuthService or UserService:
User Login:
email
andpassword
match an existing user. If the credentials are valid, the system proceeds to generate a JWT (JSON Web Token).User Registration:
In UserService, the system checks if the
email
orphoneNumber
already exists in the system. If there is a conflict (e.g., an email already in use), a custom exception such asEmailAlreadyExistsException
orPhoneAlreadyExistsException
is thrown.These exceptions are caught by the GlobalExceptionHandler, which returns a ValidationErrorResponse DTO with a specific error message.
Generate JWT Token (For Login):
Return Response:
For UserLoginRequest, the JwtResponse DTO is returned with the generated JWT token.
For UserRegisterRequest, if no conflicts are found and the registration is successful, the system returns a success message or the newly created user data.
GlobalExceptionHandler:
EmailAlreadyExistsException
), and returning appropriate ValidationErrorResponse DTO with clear error messages.Beta Was this translation helpful? Give feedback.
All reactions