Skip to content

Monerh A - #5

Open
Miuroro wants to merge 1 commit into
HackYourAssignment:mainfrom
Miuroro:main
Open

Monerh A#5
Miuroro wants to merge 1 commit into
HackYourAssignment:mainfrom
Miuroro:main

Conversation

@Miuroro

@Miuroro Miuroro commented Jul 8, 2026

Copy link
Copy Markdown

🤖 What I did:

  • Built a stateless authentication system using Spring Security and JWT.
  • Created protected /logout and /profile endpoints that require a valid JWT token.
  • Used the stateless JWT approach for logging out (client-side token removal, no server-side session logic).
  • Secured user passwords in the database using BCrypt hashing.

👾 How to test it:

  1. Run the application and open Postman.
  2. Use POST /users/register to save a new user.
  3. Use POST /auth/login with those credentials to receive a JWT token.
  4. Paste that token into the Bearer Token field in the Authorization tab to access the protected GET /users/profile and POST /auth/logout endpoints.


SecurityContextHolder.getContext().setAuthentication(authentication);
}
} catch (Exception e) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice throw exception and/or log it. Currently it is silently failing.


public LoginResponse login(LoginRequest request) {
throw new UnsupportedOperationException("TODO: implement login");
Authentication authentication = authenticationManager.authenticate(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authenticationManager.authenticate() throws AuthenticationException if credentials are invalid. Make sure you're handling that somewhere (either here with a try-catch or globally with a @ControllerAdvice) so the user gets a clean 401 response instead of a 500.

String hashedPassword = passwordEncoder.encode(request.password());

String userId = UUID.randomUUID().toString();
User newUser = new User(userId, request.username(), hashedPassword);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now if someone registers with an existing username, you'll likely get an unhandled database exception. Add a check:

if (userRepository.findByUsername(request.username()).isPresent()) {
    throw new ResponseStatusException(HttpStatus.CONFLICT, "Username already taken");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants