1 | package finki.diplomska.tripplanner.web.rest;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.User;
|
---|
4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
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;
|
---|
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;
|
---|
15 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
16 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
17 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
18 | import org.springframework.validation.BindingResult;
|
---|
19 | import org.springframework.web.bind.annotation.*;
|
---|
20 | import org.springframework.security.core.Authentication;
|
---|
21 | import javax.validation.Valid;
|
---|
22 |
|
---|
23 | @RestController
|
---|
24 | @CrossOrigin(origins = "http://localhost:4200")
|
---|
25 | @RequestMapping(value = "/api/users")
|
---|
26 | public class UserController {
|
---|
27 |
|
---|
28 | @Autowired
|
---|
29 | private MapValidationErrorService mapValidationErrorService;
|
---|
30 |
|
---|
31 | @Autowired
|
---|
32 | private UserService userService;
|
---|
33 |
|
---|
34 | @Autowired
|
---|
35 | private UserValidator userValidator;
|
---|
36 |
|
---|
37 | @Autowired
|
---|
38 | private JwtTokenProvider tokenProvider;
|
---|
39 |
|
---|
40 | @Autowired
|
---|
41 | private AuthenticationManager authenticationManager;
|
---|
42 |
|
---|
43 | @PostMapping(value = "/login")
|
---|
44 | public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult result){
|
---|
45 | ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
|
---|
46 | if(errorMap != null) return errorMap;
|
---|
47 |
|
---|
48 | Authentication authentication = authenticationManager.authenticate(
|
---|
49 | new UsernamePasswordAuthenticationToken(
|
---|
50 | loginRequest.getUsername(),
|
---|
51 | loginRequest.getPassword()
|
---|
52 | )
|
---|
53 | );
|
---|
54 |
|
---|
55 | SecurityContextHolder.getContext().setAuthentication(authentication);
|
---|
56 | String jwt = SecurityConstants.TOKEN_PREFIX + tokenProvider.generateToken(authentication);
|
---|
57 |
|
---|
58 | return ResponseEntity.ok(new JWTLoginSucessReponse(true, jwt));
|
---|
59 | }
|
---|
60 |
|
---|
61 | @PostMapping(value = "/register")
|
---|
62 | public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){
|
---|
63 | // Validate passwords match
|
---|
64 | userValidator.validate(user, result);
|
---|
65 | ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
|
---|
66 | if(errorMap != null)return errorMap;
|
---|
67 |
|
---|
68 | User newUser = userService.saveUser(user);
|
---|
69 |
|
---|
70 | return new ResponseEntity<User>(newUser, HttpStatus.CREATED);
|
---|
71 |
|
---|
72 | }
|
---|
73 | }
|
---|