Changeset 7fafead in Git for src/main/java/com/wediscussmovies/project/web/controller/UserController.java
- Timestamp:
- 01/16/22 20:22:55 (3 years ago)
- Branches:
- main
- Children:
- 3ded84d
- Parents:
- 2d57cad (diff), 7bc8942 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r2d57cad r7fafead 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.PasswordEncoder; 4 import com.wediscussmovies.project.model.User; 5 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 6 import com.wediscussmovies.project.service.UserService; 3 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PostMapping; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RequestParam; 13 14 import javax.servlet.http.HttpServletRequest; 15 import java.security.NoSuchAlgorithmException; 16 import java.util.Optional; 4 17 5 18 @Controller 19 @RequestMapping("/") 6 20 public class UserController { 21 private final UserService userService; 22 23 public UserController(UserService userService) { 24 this.userService = userService; 25 } 26 27 @GetMapping("/login") 28 public String getLoginPage(Model model){ 29 return "login"; 30 } 31 32 @PostMapping("/login/confirm") 33 public String confirmLogin(HttpServletRequest request, Model model, 34 @RequestParam String username, 35 @RequestParam String password){ 36 Optional<User> user; 37 try{ 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); 48 request.getSession().setAttribute("user", user); 49 request.getSession().setAttribute("loggedIn",true); 50 return "redirect:/movies"; 51 } 52 catch (InvalidUserCredentialsException ex){ 53 model.addAttribute("hasError", true); 54 model.addAttribute("error", ex.getMessage()); 55 return "login"; 56 } 57 } 58 59 @GetMapping("/register") 60 public String getRegisterPage(){ 61 return "register"; 62 } 63 64 @PostMapping("/login/confirm") 65 public String confirmRegister(HttpServletRequest request, 66 @RequestParam String username, 67 @RequestParam String email, 68 @RequestParam String password, 69 @RequestParam String confirmPassword, 70 @RequestParam String name, 71 @RequestParam String surname){ 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 84 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 85 if(user.isEmpty()){ 86 request.setAttribute("hasError", "true"); 87 }else{ 88 request.getSession().setAttribute("hasError", "false"); 89 request.getSession().setAttribute("user", user.get()); 90 request.getSession().setAttribute("loggedIn",true); 91 } 92 return "redirect:/movies"; 93 } 94 95 @GetMapping 96 public String logout(HttpServletRequest request){ 97 request.getSession().invalidate(); 98 return "redirect:/login"; 99 } 7 100 }
Note:
See TracChangeset
for help on using the changeset viewer.