Changeset 7bc8942 in Git
- Timestamp:
- 01/16/22 16:46:28 (3 years ago)
- Branches:
- main
- Children:
- 7fafead
- Parents:
- 2a5d6a3
- Location:
- src/main/java/com/wediscussmovies/project
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
r2a5d6a3 r7bc8942 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.PasswordEncoder; 3 4 import com.wediscussmovies.project.model.User; 4 5 import com.wediscussmovies.project.model.exception.PasswordsDontMatchException; … … 11 12 12 13 import javax.servlet.http.HttpServletRequest; 14 import java.security.NoSuchAlgorithmException; 13 15 import java.util.Optional; 14 16 … … 42 44 return Optional.empty(); 43 45 } 44 46 if(userRepository.findByUsername(username).isPresent()){ 45 47 request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage()); 46 48 return Optional.empty(); -
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r2a5d6a3 r7bc8942 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.PasswordEncoder; 3 4 import com.wediscussmovies.project.model.User; 4 5 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; … … 12 13 13 14 import javax.servlet.http.HttpServletRequest; 15 import java.security.NoSuchAlgorithmException; 14 16 import java.util.Optional; 15 17 … … 24 26 25 27 @GetMapping("/login") 26 public String getLoginPage( ){27 return "login";28 public String getLoginPage(Model model){ 29 return "login"; 28 30 } 29 31 30 32 @PostMapping("/login/confirm") 31 public String confirmLogin(HttpServletRequest request, Model model){ 33 public String confirmLogin(HttpServletRequest request, Model model, 34 @RequestParam String username, 35 @RequestParam String password){ 32 36 Optional<User> user; 33 37 try{ 34 user = this.userService.login(request.getParameter("username"), request.getParameter("password")); 38 password = PasswordEncoder.getEncodedPasswordString(password); 39 } 40 catch (NoSuchAlgorithmException ex){ 41 model.addAttribute("hasError", true); 42 model.addAttribute("error", ex.getMessage()); 43 return "login"; 44 } 45 46 try{ 47 user = this.userService.login(username, password); 35 48 request.getSession().setAttribute("user", user); 36 49 request.getSession().setAttribute("loggedIn",true); … … 58 71 @RequestParam String surname){ 59 72 Optional<User> user; 73 74 try{ 75 password = PasswordEncoder.getEncodedPasswordString(password); 76 confirmPassword = PasswordEncoder.getEncodedPasswordString(confirmPassword); 77 } 78 catch (NoSuchAlgorithmException ex){ 79 request.getSession().setAttribute("error", "Contact the administrators!"); 80 request.getSession().setAttribute("hasError", "true"); 81 return "redirect:/movies"; 82 } 83 60 84 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 61 85 if(user.isEmpty()){ 62 86 request.setAttribute("hasError", "true"); 63 87 }else{ 64 request.setAttribute("hasError", "false"); 88 request.getSession().setAttribute("hasError", "false"); 89 request.getSession().setAttribute("user", user.get()); 90 request.getSession().setAttribute("loggedIn",true); 65 91 } 66 request.getSession().setAttribute("user", user.get());67 request.getSession().setAttribute("loggedIn",true);68 92 return "redirect:/movies"; 69 93 }
Note:
See TracChangeset
for help on using the changeset viewer.