[1ad8e64] | 1 | package finki.diplomska.tripplanner.web.rest;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.User;
|
---|
| 4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
[84d0fbb] | 5 | import finki.diplomska.tripplanner.payload.JWTLoginSucessReponse;
|
---|
| 6 | import finki.diplomska.tripplanner.payload.LoginRequest;
|
---|
| 7 | import finki.diplomska.tripplanner.security.JwtTokenProvider;
|
---|
| 8 | import finki.diplomska.tripplanner.security.SecurityConstants;
|
---|
[1ad8e64] | 9 | import finki.diplomska.tripplanner.service.UserService;
|
---|
| 10 | import finki.diplomska.tripplanner.service.impl.MapValidationErrorService;
|
---|
| 11 | import finki.diplomska.tripplanner.validator.UserValidator;
|
---|
| 12 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 13 | import org.springframework.http.HttpStatus;
|
---|
| 14 | import org.springframework.http.ResponseEntity;
|
---|
[84d0fbb] | 15 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
| 16 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
| 17 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
[1ad8e64] | 18 | import org.springframework.validation.BindingResult;
|
---|
| 19 | import org.springframework.web.bind.annotation.*;
|
---|
[84d0fbb] | 20 | import org.springframework.security.core.Authentication;
|
---|
[b738035] | 21 |
|
---|
[1ad8e64] | 22 | import javax.validation.Valid;
|
---|
[b738035] | 23 | import java.util.List;
|
---|
| 24 | import java.util.Optional;
|
---|
[1ad8e64] | 25 |
|
---|
| 26 | @RestController
|
---|
| 27 | @CrossOrigin(origins = "http://localhost:4200")
|
---|
| 28 | @RequestMapping(value = "/api/users")
|
---|
| 29 | public class UserController {
|
---|
| 30 |
|
---|
| 31 | @Autowired
|
---|
| 32 | private MapValidationErrorService mapValidationErrorService;
|
---|
| 33 |
|
---|
| 34 | @Autowired
|
---|
| 35 | private UserService userService;
|
---|
| 36 |
|
---|
| 37 | @Autowired
|
---|
| 38 | private UserValidator userValidator;
|
---|
| 39 |
|
---|
[84d0fbb] | 40 | @Autowired
|
---|
| 41 | private JwtTokenProvider tokenProvider;
|
---|
| 42 |
|
---|
| 43 | @Autowired
|
---|
| 44 | private AuthenticationManager authenticationManager;
|
---|
| 45 |
|
---|
| 46 | @PostMapping(value = "/login")
|
---|
| 47 | public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult result){
|
---|
| 48 | ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
|
---|
| 49 | if(errorMap != null) return errorMap;
|
---|
| 50 |
|
---|
| 51 | Authentication authentication = authenticationManager.authenticate(
|
---|
| 52 | new UsernamePasswordAuthenticationToken(
|
---|
| 53 | loginRequest.getUsername(),
|
---|
| 54 | loginRequest.getPassword()
|
---|
| 55 | )
|
---|
| 56 | );
|
---|
| 57 | SecurityContextHolder.getContext().setAuthentication(authentication);
|
---|
| 58 | String jwt = SecurityConstants.TOKEN_PREFIX + tokenProvider.generateToken(authentication);
|
---|
| 59 | return ResponseEntity.ok(new JWTLoginSucessReponse(true, jwt));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @PostMapping(value = "/register")
|
---|
[1ad8e64] | 63 | public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){
|
---|
| 64 | // Validate passwords match
|
---|
| 65 | userValidator.validate(user, result);
|
---|
| 66 | ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
|
---|
| 67 | if(errorMap != null)return errorMap;
|
---|
| 68 | User newUser = userService.saveUser(user);
|
---|
| 69 | return new ResponseEntity<User>(newUser, HttpStatus.CREATED);
|
---|
[b738035] | 70 | }
|
---|
[84d0fbb] | 71 |
|
---|
[b738035] | 72 | @GetMapping(value = "/usernames")
|
---|
| 73 | public List<String> getAllUsernames (){
|
---|
| 74 | return this.userService.getAllUsernames();
|
---|
[1ad8e64] | 75 | }
|
---|
[b738035] | 76 |
|
---|
| 77 | @GetMapping(value = "/password")
|
---|
| 78 | public Optional<String> getPassword(@RequestBody UserDto userDto){
|
---|
| 79 |
|
---|
| 80 | return this.userService.getPassword(userDto);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1ad8e64] | 83 | }
|
---|