Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Controller
- public class LoginController {
- @Autowired
- private UserService userService;
- @Autowired
- private BCryptPasswordEncoder bCryptPasswordEncoder;
- @RequestMapping(path="/login", method=RequestMethod.GET)
- public ModelAndView login(){
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.setViewName("login");
- return modelAndView;
- }
- @RequestMapping(path="/api_endpoint/login",method=RequestMethod.POST)
- @ResponseBody
- public UserApiResponse loginUser(@RequestBody User user) {
- UserApiResponse userApiResponse=new UserApiResponse();
- User userExists = userService.findUserByEmail(user.getEmail());
- if (userExists == null) {
- userApiResponse.setMessageError("There is not any user with the email provided");
- userApiResponse.setHttpCode(400);
- }else if(!bCryptPasswordEncoder.matches(user.getPassword(), userExists.getPassword())){
- userApiResponse.setMessageError("Incorrect password");
- userApiResponse.setHttpCode(401);
- }else{
- userApiResponse.setHttpCode(200);
- }
- return userApiResponse;
- }
- @RequestMapping(path="/registration",method=RequestMethod.GET)
- public ModelAndView registration(){
- ModelAndView modelAndView = new ModelAndView();
- User user = new User();
- modelAndView.addObject("user", user);
- modelAndView.setViewName("registration");
- return modelAndView;
- }
- @RequestMapping(path="/registration",method=RequestMethod.POST)
- public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
- ModelAndView modelAndView = new ModelAndView();
- User userExists = userService.findUserByEmail(user.getEmail());
- if (userExists != null) {
- bindingResult
- .rejectValue("email", "error.user",
- "There is already a user registered with the email provided");
- }
- if (bindingResult.hasErrors()) {
- modelAndView.setViewName("registration");
- } else {
- userService.saveUser(user);
- modelAndView.addObject("successMessage", "User has been registered successfully");
- modelAndView.addObject("user", new User());
- modelAndView.setViewName("registration");
- }
- return modelAndView;
- }
Add Comment
Please, Sign In to add comment