|
| 1 | +package org.launchcode.codingevents.controllers; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServletRequest; |
| 4 | +import jakarta.servlet.http.HttpSession; |
| 5 | +import jakarta.validation.Valid; |
| 6 | +import org.launchcode.codingevents.data.UserRepository; |
| 7 | +import org.launchcode.codingevents.models.User; |
| 8 | +import org.launchcode.codingevents.models.dto.LoginFormDTO; |
| 9 | +import org.launchcode.codingevents.models.dto.RegisterFormDTO; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.stereotype.Controller; |
| 12 | +import org.springframework.validation.Errors; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.ui.Model; |
| 15 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 16 | +import org.springframework.web.bind.annotation.PostMapping; |
| 17 | + |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +@Controller |
| 21 | +public class AuthenticationController { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + UserRepository userRepository; |
| 25 | + |
| 26 | + private static final String userSessionKey = "user"; |
| 27 | + |
| 28 | + public User getUserFromSession(HttpSession session) { |
| 29 | + Integer userId = (Integer) session.getAttribute(userSessionKey); |
| 30 | + if (userId == null) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + Optional<User> user = userRepository.findById(userId); |
| 35 | + |
| 36 | + if (user.isEmpty()) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + return user.get(); |
| 41 | + } |
| 42 | + |
| 43 | + private static void setUserInSession(HttpSession session, User user) { |
| 44 | + session.setAttribute(userSessionKey, user.getId()); |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping("/register") |
| 48 | + public String displayRegistrationForm(Model model) { |
| 49 | + model.addAttribute(new RegisterFormDTO()); |
| 50 | + model.addAttribute("title", "Register"); |
| 51 | + return "register"; |
| 52 | + } |
| 53 | + |
| 54 | + @PostMapping("/register") |
| 55 | + public String processRegistrationForm(@ModelAttribute @Valid RegisterFormDTO registerFormDTO, |
| 56 | + Errors errors, HttpServletRequest request, |
| 57 | + Model model) { |
| 58 | + |
| 59 | + if (errors.hasErrors()) { |
| 60 | + model.addAttribute("title", "Register"); |
| 61 | + return "register"; |
| 62 | + } |
| 63 | + |
| 64 | + User existingUser = userRepository.findByUsername(registerFormDTO.getUsername()); |
| 65 | + |
| 66 | + if (existingUser != null) { |
| 67 | + errors.rejectValue("username", "username.alreadyexists", "A user with that username already exists"); |
| 68 | + model.addAttribute("title", "Register"); |
| 69 | + return "register"; |
| 70 | + } |
| 71 | + |
| 72 | + String password = registerFormDTO.getPassword(); |
| 73 | + String verifyPassword = registerFormDTO.getVerifyPassword(); |
| 74 | + if (!password.equals(verifyPassword)) { |
| 75 | + errors.rejectValue("password", "passwords.mismatch", "Passwords do not match"); |
| 76 | + model.addAttribute("title", "Register"); |
| 77 | + return "register"; |
| 78 | + } |
| 79 | + |
| 80 | + User newUser = new User(registerFormDTO.getUsername(), registerFormDTO.getPassword()); |
| 81 | + userRepository.save(newUser); |
| 82 | + setUserInSession(request.getSession(), newUser); |
| 83 | + |
| 84 | + return "redirect:"; |
| 85 | + } |
| 86 | + |
| 87 | + @GetMapping("/login") |
| 88 | + public String displayLoginForm(Model model) { |
| 89 | + model.addAttribute(new LoginFormDTO()); |
| 90 | + model.addAttribute("title", "Log In"); |
| 91 | + return "login"; |
| 92 | + } |
| 93 | + |
| 94 | + @PostMapping("/login") |
| 95 | + public String processLoginForm(@ModelAttribute @Valid LoginFormDTO loginFormDTO, |
| 96 | + Errors errors, HttpServletRequest request, |
| 97 | + Model model) { |
| 98 | + |
| 99 | + if (errors.hasErrors()) { |
| 100 | + model.addAttribute("title", "Log In"); |
| 101 | + return "login"; |
| 102 | + } |
| 103 | + |
| 104 | + User theUser = userRepository.findByUsername(loginFormDTO.getUsername()); |
| 105 | + |
| 106 | + if (theUser == null) { |
| 107 | + errors.rejectValue("username", "user.invalid", "The given username does not exist"); |
| 108 | + model.addAttribute("title", "Log In"); |
| 109 | + return "login"; |
| 110 | + } |
| 111 | + |
| 112 | + String password = loginFormDTO.getPassword(); |
| 113 | + |
| 114 | + if (!theUser.isMatchingPassword(password)) { |
| 115 | + errors.rejectValue("password", "password.invalid", "Invalid password"); |
| 116 | + model.addAttribute("title", "Log In"); |
| 117 | + return "login"; |
| 118 | + } |
| 119 | + |
| 120 | + setUserInSession(request.getSession(), theUser); |
| 121 | + |
| 122 | + return "redirect:"; |
| 123 | + } |
| 124 | + |
| 125 | + @GetMapping("/logout") |
| 126 | + public String logout(HttpServletRequest request){ |
| 127 | + request.getSession().invalidate(); |
| 128 | + return "redirect:/login"; |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments