Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. @Controller
  2. public class LoginController {
  3.  
  4. @Autowired
  5. private UserService userService;
  6.  
  7. @Autowired
  8. private BCryptPasswordEncoder bCryptPasswordEncoder;
  9.  
  10. @RequestMapping(path="/login", method=RequestMethod.GET)
  11. public ModelAndView login(){
  12. ModelAndView modelAndView = new ModelAndView();
  13. modelAndView.setViewName("login");
  14. return modelAndView;
  15. }
  16.  
  17. @RequestMapping(path="/api_endpoint/login",method=RequestMethod.POST)
  18. @ResponseBody
  19. public UserApiResponse loginUser(@RequestBody User user) {
  20. UserApiResponse userApiResponse=new UserApiResponse();
  21. User userExists = userService.findUserByEmail(user.getEmail());
  22. if (userExists == null) {
  23. userApiResponse.setMessageError("There is not any user with the email provided");
  24. userApiResponse.setHttpCode(400);
  25. }else if(!bCryptPasswordEncoder.matches(user.getPassword(), userExists.getPassword())){
  26. userApiResponse.setMessageError("Incorrect password");
  27. userApiResponse.setHttpCode(401);
  28. }else{
  29. userApiResponse.setHttpCode(200);
  30. }
  31.  
  32. return userApiResponse;
  33. }
  34.  
  35.  
  36. @RequestMapping(path="/registration",method=RequestMethod.GET)
  37. public ModelAndView registration(){
  38. ModelAndView modelAndView = new ModelAndView();
  39. User user = new User();
  40. modelAndView.addObject("user", user);
  41. modelAndView.setViewName("registration");
  42. return modelAndView;
  43. }
  44.  
  45. @RequestMapping(path="/registration",method=RequestMethod.POST)
  46. public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
  47. ModelAndView modelAndView = new ModelAndView();
  48. User userExists = userService.findUserByEmail(user.getEmail());
  49. if (userExists != null) {
  50. bindingResult
  51. .rejectValue("email", "error.user",
  52. "There is already a user registered with the email provided");
  53. }
  54. if (bindingResult.hasErrors()) {
  55. modelAndView.setViewName("registration");
  56. } else {
  57. userService.saveUser(user);
  58. modelAndView.addObject("successMessage", "User has been registered successfully");
  59. modelAndView.addObject("user", new User());
  60. modelAndView.setViewName("registration");
  61.  
  62. }
  63. return modelAndView;
  64. }
Add Comment
Please, Sign In to add comment