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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
AuthControllerendpoint: user login or user registration.For the loginUser() method, the request will contain a UserLoginRequest DTO, typically including the user's
emailandpassword.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:
emailandpasswordfields 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, andphoneNumberfields undergo validation:@NotBlankensures thenameandemailare not empty.@Sizeensures thepasswordmeets the required length.Custom validations are applied to ensure the
emailformat 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:
emailandpasswordmatch 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
emailorphoneNumberalready exists in the system. If there is a conflict (e.g., an email already in use), a custom exception such asEmailAlreadyExistsExceptionorPhoneAlreadyExistsExceptionis 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.All reactions