Class AuthController
This controller provides endpoints for user authentication operations including user registration (signup) and login (signin). It manages JWT token generation for authenticated users and handles user role assignments during registration. The endpoints in this controller are publicly accessible without authentication.
- Since:
- 2025-05-14
-
Constructor Summary
ConstructorsConstructorDescriptionAuthController(org.springframework.security.authentication.AuthenticationManager authenticationManager, UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder encoder, JwtUtils jwtUtils) Constructs an AuthController with the specified dependencies. -
Method Summary
Modifier and TypeMethodDescriptionauthenticateUser(@Valid LoginDto loginRequest) Authenticates a user and generates a JWT token.registerUser(@Valid RegisterDto signUpRequest) Registers a new user in the system.
-
Constructor Details
-
AuthController
public AuthController(org.springframework.security.authentication.AuthenticationManager authenticationManager, UserRepository userRepository, org.springframework.security.crypto.password.PasswordEncoder encoder, JwtUtils jwtUtils) Constructs an AuthController with the specified dependencies.- Parameters:
authenticationManager- The manager for authenticating user credentialsuserRepository- The repository for user data operationsencoder- The password encoder for securely hashing passwordsjwtUtils- Utility for JWT token generation and validation
-
-
Method Details
-
authenticateUser
@PostMapping("/signin") public ResponseEntity<?> authenticateUser(@Valid @RequestBody @Valid LoginDto loginRequest) Authenticates a user and generates a JWT token.This endpoint processes login requests by validating the provided credentials against the stored user data. Upon successful authentication, it generates a JWT token that can be used for subsequent authorized API requests. The response includes the token, user ID, username, email, and assigned roles.
- Parameters:
loginRequest- The DTO containing login credentials (username and password)- Returns:
- ResponseEntity with JWT token and user information on success
-
registerUser
@PostMapping("/signup") public ResponseEntity<?> registerUser(@Valid @RequestBody @Valid RegisterDto signUpRequest) Registers a new user in the system.This endpoint handles user registration by validating the request data, checking for existing username or email conflicts, and creating a new user account with the specified roles. If no role is specified, the user is assigned the default CANDIDATE role. The password is securely encoded before storage.
- Parameters:
signUpRequest- The DTO containing registration details (username, email, password, roles)- Returns:
- ResponseEntity with success message or error details
-