Ignore:
Timestamp:
01/16/22 16:46:28 (3 years ago)
Author:
Petar Partaloski <ppartaloski@…>
Branches:
main
Children:
7fafead
Parents:
2a5d6a3
Message:

Fixed password encoding

File:
1 edited

Legend:

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

    r2a5d6a3 r7bc8942  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.model.PasswordEncoder;
    34import com.wediscussmovies.project.model.User;
    45import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException;
     
    1213
    1314import javax.servlet.http.HttpServletRequest;
     15import java.security.NoSuchAlgorithmException;
    1416import java.util.Optional;
    1517
     
    2426
    2527    @GetMapping("/login")
    26     public String getLoginPage(){
    27        return "login";
     28    public String getLoginPage(Model model){
     29        return "login";
    2830    }
    2931
    3032    @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){
    3236        Optional<User> user;
    3337        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);
    3548            request.getSession().setAttribute("user", user);
    3649            request.getSession().setAttribute("loggedIn",true);
     
    5871                                  @RequestParam String surname){
    5972        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
    6084        user = this.userService.register(request, email, password, confirmPassword, username, name, surname);
    6185        if(user.isEmpty()){
    6286            request.setAttribute("hasError", "true");
    6387        }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);
    6591        }
    66         request.getSession().setAttribute("user", user.get());
    67         request.getSession().setAttribute("loggedIn",true);
    6892        return "redirect:/movies";
    6993    }
Note: See TracChangeset for help on using the changeset viewer.