Ignore:
Timestamp:
01/16/22 20:22:55 (3 years ago)
Author:
Test <matonikolov77@…>
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.
Message:

Resolving models

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/web/controller/UserController.java

    r2d57cad r7fafead  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.model.PasswordEncoder;
     4import com.wediscussmovies.project.model.User;
     5import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException;
     6import com.wediscussmovies.project.service.UserService;
    37import org.springframework.stereotype.Controller;
     8import org.springframework.ui.Model;
     9import org.springframework.web.bind.annotation.GetMapping;
     10import org.springframework.web.bind.annotation.PostMapping;
     11import org.springframework.web.bind.annotation.RequestMapping;
     12import org.springframework.web.bind.annotation.RequestParam;
     13
     14import javax.servlet.http.HttpServletRequest;
     15import java.security.NoSuchAlgorithmException;
     16import java.util.Optional;
    417
    518@Controller
     19@RequestMapping("/")
    620public 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    }
    7100}
Note: See TracChangeset for help on using the changeset viewer.