[6e7b472] | 1 | package com.wediscussmovies.project.web.controller;
|
---|
| 2 |
|
---|
[7bc8942] | 3 | import com.wediscussmovies.project.model.PasswordEncoder;
|
---|
[2a5d6a3] | 4 | import com.wediscussmovies.project.model.User;
|
---|
| 5 | import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException;
|
---|
| 6 | import com.wediscussmovies.project.service.UserService;
|
---|
[6e7b472] | 7 | import org.springframework.stereotype.Controller;
|
---|
[2a5d6a3] | 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;
|
---|
[7bc8942] | 15 | import java.security.NoSuchAlgorithmException;
|
---|
[2a5d6a3] | 16 | import java.util.Optional;
|
---|
[6e7b472] | 17 |
|
---|
| 18 | @Controller
|
---|
[2a5d6a3] | 19 | @RequestMapping("/")
|
---|
[6e7b472] | 20 | public class UserController {
|
---|
[2a5d6a3] | 21 | private final UserService userService;
|
---|
| 22 |
|
---|
| 23 | public UserController(UserService userService) {
|
---|
| 24 | this.userService = userService;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | @GetMapping("/login")
|
---|
[7bc8942] | 28 | public String getLoginPage(Model model){
|
---|
| 29 | return "login";
|
---|
[2a5d6a3] | 30 | }
|
---|
| 31 |
|
---|
| 32 | @PostMapping("/login/confirm")
|
---|
[7bc8942] | 33 | public String confirmLogin(HttpServletRequest request, Model model,
|
---|
| 34 | @RequestParam String username,
|
---|
| 35 | @RequestParam String password){
|
---|
[2a5d6a3] | 36 | Optional<User> user;
|
---|
| 37 | try{
|
---|
[7bc8942] | 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);
|
---|
[2a5d6a3] | 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;
|
---|
[7bc8942] | 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 |
|
---|
[2a5d6a3] | 84 | user = this.userService.register(request, email, password, confirmPassword, username, name, surname);
|
---|
| 85 | if(user.isEmpty()){
|
---|
| 86 | request.setAttribute("hasError", "true");
|
---|
| 87 | }else{
|
---|
[7bc8942] | 88 | request.getSession().setAttribute("hasError", "false");
|
---|
| 89 | request.getSession().setAttribute("user", user.get());
|
---|
| 90 | request.getSession().setAttribute("loggedIn",true);
|
---|
[2a5d6a3] | 91 | }
|
---|
| 92 | return "redirect:/movies";
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @GetMapping
|
---|
| 96 | public String logout(HttpServletRequest request){
|
---|
| 97 | request.getSession().invalidate();
|
---|
| 98 | return "redirect:/login";
|
---|
| 99 | }
|
---|
[6e7b472] | 100 | }
|
---|