33import com .codesignal .pastebin .model .Role ;
44import com .codesignal .pastebin .model .User ;
55import com .codesignal .pastebin .repo .UserRepository ;
6+ import com .codesignal .pastebin .util .ErrorResponse ;
67import com .codesignal .pastebin .util .JwtUtil ;
78import org .springframework .http .HttpStatus ;
89import org .springframework .http .ResponseEntity ;
9- import org .springframework .security .crypto .bcrypt . BCryptPasswordEncoder ;
10- import org .springframework .web .bind .annotation .* ;
11-
12- import java . util . HashMap ;
13- import java . util . Map ;
10+ import org .springframework .security .crypto .password . PasswordEncoder ;
11+ import org .springframework .web .bind .annotation .PostMapping ;
12+ import org . springframework . web . bind . annotation . RequestBody ;
13+ import org . springframework . web . bind . annotation . RequestMapping ;
14+ import org . springframework . web . bind . annotation . RestController ;
1415
1516@ RestController
1617@ RequestMapping ("/api/auth" )
1718public class AuthController {
1819 private final UserRepository users ;
1920 private final JwtUtil jwt ;
20- private final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder () ;
21+ private final PasswordEncoder encoder ;
2122
22- public AuthController (UserRepository users , JwtUtil jwt ) {
23+ public AuthController (UserRepository users , JwtUtil jwt , PasswordEncoder encoder ) {
2324 this .users = users ;
2425 this .jwt = jwt ;
26+ this .encoder = encoder ;
2527 }
2628
2729 @ PostMapping ("/register" )
28- public ResponseEntity <?> register (@ RequestBody Map < String , Object > body ) {
29- String username = ( String ) body . get ( " username" );
30- String password = ( String ) body . get ( " password" );
31- String roleRaw = ( String ) body . getOrDefault ( "role" , "user" );
30+ public ResponseEntity <?> register (@ RequestBody RegisterRequest request ) {
31+ String username = request . username ( );
32+ String password = request . password ( );
33+ String roleRaw = request . role () == null ? "user" : request . role ( );
3234
3335 if (username == null || password == null ) {
3436 return error (HttpStatus .BAD_REQUEST , "Username and password are required" );
@@ -38,41 +40,46 @@ public ResponseEntity<?> register(@RequestBody Map<String, Object> body) {
3840 return error (HttpStatus .BAD_REQUEST , "Username already exists" );
3941 }
4042
41- Role role = "admin" .equals (roleRaw ) ? Role .ADMIN : Role .USER ;
43+ Role role = switch (roleRaw ) {
44+ case "admin" -> Role .ADMIN ;
45+ default -> Role .USER ;
46+ };
4247 User u = new User ();
4348 u .setUsername (username );
4449 u .setPassword (encoder .encode (password ));
4550 u .setRole (role );
4651 users .save (u );
4752
48- Map <String , Object > res = new HashMap <>();
49- res .put ("message" , "User registered successfully" );
50- res .put ("userId" , u .getId ());
51- return ResponseEntity .ok (res );
53+ return ResponseEntity .ok (new RegisterResponse ("User registered successfully" , u .getId ()));
5254 }
5355
5456 @ PostMapping ("/login" )
55- public ResponseEntity <?> login (@ RequestBody Map <String , Object > body ) {
56- String username = (String ) body .get ("username" );
57- String password = (String ) body .get ("password" );
58-
59- var userOpt = users .findByUsername (username );
60- if (userOpt .isPresent ()) {
61- User u = userOpt .get ();
62- if (encoder .matches (password , u .getPassword ())) {
63- String token = jwt .generateTokenWithUserId (u .getId ());
64- Map <String , Object > res = new HashMap <>();
65- res .put ("token" , token );
66- return ResponseEntity .ok (res );
67- }
57+ public ResponseEntity <?> login (@ RequestBody LoginRequest request ) {
58+ String username = request .username ();
59+ String password = request .password ();
60+
61+ User user = users .findByUsername (username ).orElse (null );
62+ if (user == null || !encoder .matches (password , user .getPassword ())) {
63+ return error (HttpStatus .UNAUTHORIZED , "Invalid credentials" );
6864 }
69- return error (HttpStatus .UNAUTHORIZED , "Invalid credentials" );
65+
66+ String token = jwt .generateTokenWithUserId (user .getId ());
67+ return ResponseEntity .ok (new TokenResponse (token ));
7068 }
7169
72- private ResponseEntity <Map <String , Object >> error (HttpStatus status , String detail ) {
73- Map <String , Object > err = new HashMap <>();
74- err .put ("detail" , detail );
75- return ResponseEntity .status (status ).body (err );
70+ private ResponseEntity <ErrorResponse > error (HttpStatus status , String detail ) {
71+ return ResponseEntity .status (status ).body (new ErrorResponse (detail ));
72+ }
73+
74+ public record RegisterRequest (String username , String password , String role ) {
7675 }
77- }
7876
77+ public record LoginRequest (String username , String password ) {
78+ }
79+
80+ public record RegisterResponse (String message , Integer userId ) {
81+ }
82+
83+ public record TokenResponse (String token ) {
84+ }
85+ }
0 commit comments