Changeset 2a5d6a3 in Git for src/main/java/com/wediscussmovies/project/web/controller/UserController.java
- Timestamp:
- 01/16/22 15:29:49 (3 years ago)
- Branches:
- main
- Children:
- 7bc8942
- Parents:
- 7a0bf79
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.User; 4 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 5 import com.wediscussmovies.project.service.UserService; 3 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 13 import javax.servlet.http.HttpServletRequest; 14 import java.util.Optional; 4 15 5 16 @Controller 17 @RequestMapping("/") 6 18 public class UserController { 19 private final UserService userService; 20 21 public UserController(UserService userService) { 22 this.userService = userService; 23 } 24 25 @GetMapping("/login") 26 public String getLoginPage(){ 27 return "login"; 28 } 29 30 @PostMapping("/login/confirm") 31 public String confirmLogin(HttpServletRequest request, Model model){ 32 Optional<User> user; 33 try{ 34 user = this.userService.login(request.getParameter("username"), request.getParameter("password")); 35 request.getSession().setAttribute("user", user); 36 request.getSession().setAttribute("loggedIn",true); 37 return "redirect:/movies"; 38 } 39 catch (InvalidUserCredentialsException ex){ 40 model.addAttribute("hasError", true); 41 model.addAttribute("error", ex.getMessage()); 42 return "login"; 43 } 44 } 45 46 @GetMapping("/register") 47 public String getRegisterPage(){ 48 return "register"; 49 } 50 51 @PostMapping("/login/confirm") 52 public String confirmRegister(HttpServletRequest request, 53 @RequestParam String username, 54 @RequestParam String email, 55 @RequestParam String password, 56 @RequestParam String confirmPassword, 57 @RequestParam String name, 58 @RequestParam String surname){ 59 Optional<User> user; 60 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 61 if(user.isEmpty()){ 62 request.setAttribute("hasError", "true"); 63 }else{ 64 request.setAttribute("hasError", "false"); 65 } 66 request.getSession().setAttribute("user", user.get()); 67 request.getSession().setAttribute("loggedIn",true); 68 return "redirect:/movies"; 69 } 70 71 @GetMapping 72 public String logout(HttpServletRequest request){ 73 request.getSession().invalidate(); 74 return "redirect:/login"; 75 } 7 76 }
Note:
See TracChangeset
for help on using the changeset viewer.