Skip to content

Commit

Permalink
✨ feat: LoginController에 로그인, 회원가입 로직 추가
Browse files Browse the repository at this point in the history
- 로그인, 회원가입 로직 추가
- BindingResult를 사용하여 Error 필드 활용
- 오류가 있을 시, thymeleaf와의 연계로 뷰에 에러 메시지를 출력할 수 있음
- 특정 로직 수행 시 어떤 화면으로 가야할 지 지정해둠
- 조금 번잡해서 추후 리팩토링 필요할 수 있음
  • Loading branch information
hightuv committed Mar 24, 2024
1 parent 407a728 commit 55119b7
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/main/java/com/example/WebOrder/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,85 @@
package com.example.WebOrder.controller;

import com.example.WebOrder.dto.LoginDto;
import com.example.WebOrder.dto.RegisterDto;
import com.example.WebOrder.dto.LoginFormDto;
import com.example.WebOrder.dto.UserFormDto;
import com.example.WebOrder.entity.User;
import com.example.WebOrder.service.LoginService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Slf4j
@Controller
public class LoginController {
private final LoginService loginService;
private final PasswordEncoder passwordEncoder;

public LoginController(LoginService loginService) {
public LoginController(LoginService loginService, PasswordEncoder passwordEncoder) {
this.loginService = loginService;
this.passwordEncoder = passwordEncoder;
}

@GetMapping("/index")
public String getIndex(){
return "/html/index";
}
@GetMapping("/login")
public String getLoginForm(){
public String getLoginForm(@ModelAttribute("loginFormDto") LoginFormDto dto){
log.info("로그인 폼 소환");
return "/html/login";
return "html/loginForm";
}

@PostMapping("/login")
public String login(LoginDto dto){
public String login(@Valid @ModelAttribute ("loginFormDto") LoginFormDto dto, BindingResult bindingResult){
log.info("로그인 시도");
if (loginService.isLoginAttemptValid(dto)){
log.info("로그인 성공");
return "/html/index";
return "html/index";
}
else {
log.info("로그인 실패");
return "redirect:/login?error=true";
bindingResult.reject("loginFail", "아이디 또는 비밀번호가 맞지 않습니다.");
return "html/loginForm";
}
}

@GetMapping("/register")
public String getRegisterForm(){
public String registerForm(@ModelAttribute("userFormDto") UserFormDto dto) {
log.info("회원가입 폼 소환");
return "/html/register";
return "html/registerForm";
}

@PostMapping("/register")
public String register(RegisterDto dto){
public String register(@Valid UserFormDto dto, BindingResult bindingResult){
log.info("회원가입 시도");
if (loginService.usernameExists(dto.getUsername())){
if (bindingResult.hasErrors()) {
log.info("회원가입 실패");
return "html/registerForm";
}

if (loginService.findByUsername(dto.getUsername()).isPresent()){
log.info("username 중복");
return "redirect:/register?error=true";
}

if (!dto.getPassword().equals(dto.getPasswordCheck())) {
bindingResult.rejectValue("passwordCheck", "passwordNotSame", "비밀번호를 다시 확인해주세요.");
return "html/registerForm";
}
else {
log.info("회원가입 성공");
loginService.createUser(dto);
return "redirect:/login";
User user = new User();
user.setUsername(dto.getUsername());
user.setPassword(passwordEncoder.encode(dto.getPassword()));
user.setName(dto.getName());
loginService.join(user);
return "redirect:/";
}
}
}

0 comments on commit 55119b7

Please sign in to comment.