|
36 | 36 | @RestController |
37 | 37 | @RequestMapping("/api/auth") |
38 | 38 | public class AuthController { |
39 | | - @Autowired |
40 | | - AuthenticationManager authenticationManager; |
41 | | - |
42 | | - @Autowired |
43 | | - UserRepository userRepository; |
44 | | - |
45 | | - @Autowired |
46 | | - RoleRepository roleRepository; |
47 | | - |
48 | | - @Autowired |
49 | | - PasswordEncoder encoder; |
50 | | - |
51 | | - @Autowired |
52 | | - JwtUtils jwtUtils; |
53 | | - |
54 | | - @PostMapping("/signin") |
55 | | - public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { |
56 | | - |
57 | | - Authentication authentication = authenticationManager.authenticate( |
58 | | - new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); |
59 | | - |
60 | | - SecurityContextHolder.getContext().setAuthentication(authentication); |
61 | | - String jwt = jwtUtils.generateJwtToken(authentication); |
62 | | - |
63 | | - UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal(); |
64 | | - List<String> roles = userDetails.getAuthorities().stream() |
65 | | - .map(item -> item.getAuthority()) |
66 | | - .collect(Collectors.toList()); |
67 | | - |
68 | | - return ResponseEntity.ok(new JwtResponse(jwt, |
69 | | - userDetails.getId(), |
70 | | - userDetails.getUsername(), |
71 | | - userDetails.getEmail(), |
72 | | - roles)); |
73 | | - } |
74 | | - |
75 | | - @PostMapping("/signup") |
76 | | - public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRequest) { |
77 | | - if (userRepository.existsByUsername(signUpRequest.getUsername())) { |
78 | | - return ResponseEntity |
79 | | - .badRequest() |
80 | | - .body(new MessageResponse("Error: Username is already taken!")); |
81 | | - } |
82 | | - |
83 | | - if (userRepository.existsByEmail(signUpRequest.getEmail())) { |
84 | | - return ResponseEntity |
85 | | - .badRequest() |
86 | | - .body(new MessageResponse("Error: Email is already in use!")); |
87 | | - } |
88 | | - |
89 | | - // Create new user's account |
90 | | - User user = new User(signUpRequest.getUsername(), |
91 | | - signUpRequest.getEmail(), |
92 | | - encoder.encode(signUpRequest.getPassword())); |
93 | | - |
94 | | - Set<String> strRoles = signUpRequest.getRole(); |
95 | | - Set<Role> roles = new HashSet<>(); |
96 | | - |
97 | | - if (strRoles == null) { |
98 | | - Role userRole = roleRepository.findByName(ERole.ROLE_USER) |
99 | | - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
100 | | - roles.add(userRole); |
101 | | - } else { |
102 | | - strRoles.forEach(role -> { |
103 | | - switch (role) { |
104 | | - case "admin": |
105 | | - Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) |
106 | | - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
107 | | - roles.add(adminRole); |
108 | | - |
109 | | - break; |
110 | | - case "mod": |
111 | | - Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) |
112 | | - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
113 | | - roles.add(modRole); |
114 | | - |
115 | | - break; |
116 | | - default: |
117 | | - Role userRole = roleRepository.findByName(ERole.ROLE_USER) |
118 | | - .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
119 | | - roles.add(userRole); |
120 | | - } |
121 | | - }); |
122 | | - } |
123 | | - |
124 | | - user.setRoles(roles); |
125 | | - userRepository.save(user); |
126 | | - |
127 | | - return ResponseEntity.ok(new MessageResponse("User registered successfully!")); |
128 | | - } |
| 39 | + @Autowired |
| 40 | + AuthenticationManager authenticationManager; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + UserRepository userRepository; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + RoleRepository roleRepository; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + PasswordEncoder encoder; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + JwtUtils jwtUtils; |
| 53 | + |
| 54 | + @PostMapping("/signin") |
| 55 | + public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { |
| 56 | + |
| 57 | + Authentication authentication = authenticationManager.authenticate( |
| 58 | + new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); |
| 59 | + |
| 60 | + SecurityContextHolder.getContext().setAuthentication(authentication); |
| 61 | + String jwt = jwtUtils.generateJwtToken(authentication); |
| 62 | + |
| 63 | + UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal(); |
| 64 | + List<String> roles = userDetails.getAuthorities().stream() |
| 65 | + .map(item -> item.getAuthority()) |
| 66 | + .collect(Collectors.toList()); |
| 67 | + |
| 68 | + return ResponseEntity.ok(new JwtResponse(jwt, |
| 69 | + userDetails.getId(), |
| 70 | + userDetails.getUsername(), |
| 71 | + userDetails.getEmail(), |
| 72 | + roles)); |
| 73 | + } |
| 74 | + |
| 75 | + @PostMapping("/signup") |
| 76 | + public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRequest) { |
| 77 | + if (userRepository.existsByUsername(signUpRequest.getUsername())) { |
| 78 | + return ResponseEntity |
| 79 | + .badRequest() |
| 80 | + .body(new MessageResponse("Error: Username is already taken!")); |
| 81 | + } |
| 82 | + |
| 83 | + if (userRepository.existsByEmail(signUpRequest.getEmail())) { |
| 84 | + return ResponseEntity |
| 85 | + .badRequest() |
| 86 | + .body(new MessageResponse("Error: Email is already in use!")); |
| 87 | + } |
| 88 | + |
| 89 | + // Create new user's account |
| 90 | + User user = new User(signUpRequest.getUsername(), |
| 91 | + signUpRequest.getEmail(), |
| 92 | + encoder.encode(signUpRequest.getPassword())); |
| 93 | + |
| 94 | + Set<String> strRoles = signUpRequest.getRole(); |
| 95 | + Set<Role> roles = new HashSet<>(); |
| 96 | + |
| 97 | + if (strRoles == null) { |
| 98 | + Role userRole = roleRepository.findByName(ERole.ROLE_USER) |
| 99 | + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
| 100 | + roles.add(userRole); |
| 101 | + } else { |
| 102 | + strRoles.forEach(role -> { |
| 103 | + switch (role) { |
| 104 | + case "admin": |
| 105 | + Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) |
| 106 | + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
| 107 | + roles.add(adminRole); |
| 108 | + |
| 109 | + break; |
| 110 | + case "mod": |
| 111 | + Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) |
| 112 | + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
| 113 | + roles.add(modRole); |
| 114 | + |
| 115 | + break; |
| 116 | + default: |
| 117 | + Role userRole = roleRepository.findByName(ERole.ROLE_USER) |
| 118 | + .orElseThrow(() -> new RuntimeException("Error: Role is not found.")); |
| 119 | + roles.add(userRole); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + user.setRoles(roles); |
| 125 | + userRepository.save(user); |
| 126 | + |
| 127 | + return ResponseEntity.ok(new MessageResponse("User registered successfully!")); |
| 128 | + } |
129 | 129 | } |
0 commit comments