From 465bf43ded10de44419975dd71c86d7db7447a5d Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:19:49 +0900 Subject: [PATCH 01/13] =?UTF-8?q?:sparkles:=20feat:=20Seat=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20User=20=ED=95=84=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Seat 엔티티에 User 필드 추가 - Seat 엔티티와 User 연관관계 설정 --- src/main/java/com/example/WebOrder/entity/Seat.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/example/WebOrder/entity/Seat.java b/src/main/java/com/example/WebOrder/entity/Seat.java index bcda4fe70a..c06c5d762c 100644 --- a/src/main/java/com/example/WebOrder/entity/Seat.java +++ b/src/main/java/com/example/WebOrder/entity/Seat.java @@ -7,6 +7,8 @@ import java.util.ArrayList; import java.util.List; +import static jakarta.persistence.FetchType.LAZY; + @Entity @Getter @Setter public class Seat { @@ -17,6 +19,10 @@ public class Seat { private String name; + @ManyToOne(fetch = LAZY) + @JoinColumn(name = "user_id") + private User user; + @OneToMany(mappedBy = "seat", cascade = CascadeType.ALL) private List orders = new ArrayList<>(); From c119642ab361ef92effbcd7ede70ce368e4e2e40 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:21:33 +0900 Subject: [PATCH 02/13] =?UTF-8?q?:sparkles:=20feat:=20SecurityConfig=20req?= =?UTF-8?q?uestMatchers=20=EA=B2=BD=EB=A1=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - css 경로 추가 - image 경로 추가 --- src/main/java/com/example/WebOrder/config/SecurityConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/WebOrder/config/SecurityConfig.java b/src/main/java/com/example/WebOrder/config/SecurityConfig.java index e6f5bfb069..7671e33dbf 100644 --- a/src/main/java/com/example/WebOrder/config/SecurityConfig.java +++ b/src/main/java/com/example/WebOrder/config/SecurityConfig.java @@ -26,7 +26,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti http .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(authorize -> authorize - .requestMatchers("/login", "/register").permitAll() + .requestMatchers("/login", "/register", "/css/**", "/images/**") + .permitAll() .anyRequest().authenticated() ) .formLogin(formLogin -> formLogin From d5919004706beb4bad42b1763804746f540fa119 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:22:15 +0900 Subject: [PATCH 03/13] =?UTF-8?q?:sparkles:=20feat:=20User=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20name=20=ED=95=84=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 사용자 이름을 위한 필드 --- src/main/java/com/example/WebOrder/entity/User.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/WebOrder/entity/User.java b/src/main/java/com/example/WebOrder/entity/User.java index e2e76cdf09..5e45610536 100644 --- a/src/main/java/com/example/WebOrder/entity/User.java +++ b/src/main/java/com/example/WebOrder/entity/User.java @@ -12,7 +12,7 @@ @Entity @Getter @Setter -@ToString +@ToString(of = {"id", "username"}) @EqualsAndHashCode public class User implements UserDetails { @Id @@ -26,6 +26,9 @@ public class User implements UserDetails { @Column(nullable = false) private String password; + @Column(nullable = false) + private String name; + @OneToMany(mappedBy = "user") private List seats = new ArrayList<>(); From a4f5da62383f614112410373f80b7fe12685e33a Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:54:17 +0900 Subject: [PATCH 04/13] =?UTF-8?q?:sparkles:=20feat:=20LoginService?= =?UTF-8?q?=EC=97=90=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 회원가입 로직 추가 --- .../WebOrder/service/LoginService.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/example/WebOrder/service/LoginService.java b/src/main/java/com/example/WebOrder/service/LoginService.java index 4229e25789..75439d7678 100644 --- a/src/main/java/com/example/WebOrder/service/LoginService.java +++ b/src/main/java/com/example/WebOrder/service/LoginService.java @@ -1,10 +1,9 @@ package com.example.WebOrder.service; -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.repository.UserRepository; -import org.apache.tomcat.websocket.AuthenticationException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; @@ -26,34 +25,23 @@ public LoginService(UserRepository userRepository, PasswordEncoder passwordEncod this.passwordEncoder = passwordEncoder; } - - - @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return userRepository.findByUsername(username).get(); } - public Boolean isLoginAttemptValid(LoginDto dto){ - Optional optionalUser = userRepository.findByUsername(dto.getUsername()); + public Boolean isLoginAttemptValid(LoginFormDto dto){ + Optional findUser = userRepository.findByUsername(dto.getUsername()); - if (optionalUser.isEmpty()) throw new UsernameNotFoundException("유저가 존재하지 않습니다."); + if (findUser.isEmpty()) throw new UsernameNotFoundException("유저가 존재하지 않습니다."); - User user = optionalUser.get(); + User user = findUser.get(); if (!passwordEncoder.matches(dto.getPassword(), user.getPassword())) throw new RuntimeException("비밀번호가 일치하지 않습니다."); return true; } - public User createUser(RegisterDto dto){ - User user = new User(); - user.setUsername(dto.getUsername()); - user.setPassword(passwordEncoder.encode(dto.getPassword())); - - return userRepository.save(user); - } - public Boolean usernameExists(String username){ return userRepository.existsByUsername(username); } @@ -66,4 +54,17 @@ public Boolean isCurrentUserAuthenticated(Long userId){ } return false; } + + /** + * 회원가입 + */ + public Long join(User user) { + userRepository.save(user); + return user.getId(); + } + + public Optional findByUsername(String username) { + return userRepository.findByUsername(username); + } } + From d169d3895115276f28366b308dc5f9d835bef3ad Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:54:46 +0900 Subject: [PATCH 05/13] =?UTF-8?q?:recycle:=20refactor:=20LoginDto=EB=A5=BC?= =?UTF-8?q?=20LoginFormDto=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 리팩토링 - username, password에 NotEmpty Constraint 적용 --- .../WebOrder/dto/{LoginDto.java => LoginFormDto.java} | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) rename src/main/java/com/example/WebOrder/dto/{LoginDto.java => LoginFormDto.java} (64%) diff --git a/src/main/java/com/example/WebOrder/dto/LoginDto.java b/src/main/java/com/example/WebOrder/dto/LoginFormDto.java similarity index 64% rename from src/main/java/com/example/WebOrder/dto/LoginDto.java rename to src/main/java/com/example/WebOrder/dto/LoginFormDto.java index b1ce1031aa..7c87af61ca 100644 --- a/src/main/java/com/example/WebOrder/dto/LoginDto.java +++ b/src/main/java/com/example/WebOrder/dto/LoginFormDto.java @@ -1,5 +1,6 @@ package com.example.WebOrder.dto; +import jakarta.validation.constraints.NotEmpty; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -7,7 +8,9 @@ @Getter @Setter @ToString -public class LoginDto { +public class LoginFormDto { + @NotEmpty private String username; + @NotEmpty private String password; } From 009a24f2db70982cf4b97b25cd783ac1eed345d8 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:55:22 +0900 Subject: [PATCH 06/13] =?UTF-8?q?:lipstick:=20assets:=20registerSuccess.ht?= =?UTF-8?q?ml=20=EC=B6=94=EA=B0=80=20(=EC=9E=84=EC=8B=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 나중에 사용할 용도 - 현재는 회원가입 완료 시 로그인 화면으로 바로 돌아감 --- src/main/resources/templates/html/registerSuccess.html | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/resources/templates/html/registerSuccess.html diff --git a/src/main/resources/templates/html/registerSuccess.html b/src/main/resources/templates/html/registerSuccess.html new file mode 100644 index 0000000000..5d0514c237 --- /dev/null +++ b/src/main/resources/templates/html/registerSuccess.html @@ -0,0 +1,10 @@ + + + + + 회원가입 성공 + + + + + \ No newline at end of file From 7fc652c84f0a2e24b3de471403ed31bbdd3a9d3a Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:55:48 +0900 Subject: [PATCH 07/13] =?UTF-8?q?:lipstick:=20assets:=20=EB=92=A4=EB=A1=9C?= =?UTF-8?q?=EA=B0=80=EA=B8=B0=20img=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 뒤로가기 아이콘 --- src/main/resources/static/images/previous.png | Bin 0 -> 366 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/resources/static/images/previous.png diff --git a/src/main/resources/static/images/previous.png b/src/main/resources/static/images/previous.png new file mode 100644 index 0000000000000000000000000000000000000000..654e509672eb03837c675f860de66d9723056a21 GIT binary patch literal 366 zcmeAS@N?(olHy`uVBq!ia0vp^Q6S901|%(3I5Gh#&H|6fVg?3oVGw3ym^DX&fq_xM z)5S5QV$R!Jr@ambh_qddXU{t5F0QFm!7P8s@#E4x57r(&)!4IV$x^l5PS=`jwV!+l z^qX{94QLz$d^nppX|f8x=eMoG(}+2T`=_uqL|x!|?Wtz9Q_ z{mS>A&~%9_nV;;1i(C!I0T94?dlquFP_?f=(`Bd&%MQ^UFRPT;^B9f&4 zyN^p!@7vlLkB`hNzkPI5ca6=V(idB7Z|>uP`EEki{MMdpoWYJ?R)T~)UHx3vIVCg! E0Dlsk8~^|S literal 0 HcmV?d00001 From 6de5dd8167aa3d29e334e53d67969f46ee98c7ea Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:56:13 +0900 Subject: [PATCH 08/13] =?UTF-8?q?:lipstick:=20assets:=20registerForm.html?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 회원가입 화면 구현 - thymeleaf를 사용하여 error 필드 활용 --- .../templates/html/registerForm.html | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/resources/templates/html/registerForm.html diff --git a/src/main/resources/templates/html/registerForm.html b/src/main/resources/templates/html/registerForm.html new file mode 100644 index 0000000000..b64895f76b --- /dev/null +++ b/src/main/resources/templates/html/registerForm.html @@ -0,0 +1,43 @@ + + + + + + 회원가입 + + + + +
+
+ + 회원가입 +
+
+
+ + +
+

+
+ + +
+

+
+ + +
+

+ +
+ + +
+

+ +
+ +
+ + From 7f150e9d5f0a8c61594f073cbf4475c2888e86b4 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:56:28 +0900 Subject: [PATCH 09/13] =?UTF-8?q?:lipstick:=20assets:=20registerForm.css?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - registerForm.html을 위한 css 파일 --- .../resources/static/css/registerForm.css | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/main/resources/static/css/registerForm.css diff --git a/src/main/resources/static/css/registerForm.css b/src/main/resources/static/css/registerForm.css new file mode 100644 index 0000000000..2f16a45dc4 --- /dev/null +++ b/src/main/resources/static/css/registerForm.css @@ -0,0 +1,103 @@ +.body { + overflow:hidden; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width:100vw; + height:100vh; +} +.register-form { + width:320px; + height:auto; + position:relative; + margin-top: 20px; +} +.header { + width:320px; + height: auto; + position: relative; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} + +.form-header-icon { + width: 23px; + height: 23px; + position: relative; + background-image: url(../images/previous.png); + background-color:transparent; + background-repeat: no-repeat; + background-size: cover; + border:0; + margin-right:auto; +} + +.form-header-title { + color: rgba(64, 64, 64, 1); + width: 104px; + height: 37px; + position: relative; + font-family: Noto Sans KR, sans-serif; + text-align: left; + font-size: 28px; + letter-spacing: 0; + margin-right:auto; +} + +.form-group { + width:320px; + height:79px; + position: relative; + margin-top:15px; +} + +.form-label { + color: rgba(64, 64, 64, 1); + position: relative; + font-family: Noto Sans KR, sans-serif; + text-align: left; + font-size: 16px; + letter-spacing: 0; +} + +.form-input-text { + color: rgba(189, 189, 189, 1); + width: 320px; + height: 50px; + position: relative; + font-family: Noto Sans KR, sans-serif; + text-align: left; + font-size: 14px; + letter-spacing: 0; + border-radius: 5px; + border:1px solid rgba(174.00000482797623, 174.00000482797623, 174.00000482797623, 1); +} + +.submit-button { + background-color:rgba(91.46796986460686, 96.86175003647804, 226.31249696016312, 1); + width:320px; + height:55px; + color:rgba(255, 255, 255, 1); + font-family:Noto Sans KR, sans-serif; + text-align:center; + font-size:16px; + letter-spacing:0; + position:relative; + margin-top: 20%; +} +.field-error { + border-color: #dc3545; + color: #dc3545; + font-family:Noto Sans KR, sans-serif; + font-size:12px; + letter-spacing:0; +} \ No newline at end of file From 366051d8d1f3059e92bc5a4eed49753d51d6d4dc Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:58:01 +0900 Subject: [PATCH 10/13] =?UTF-8?q?:sparkles:=20feat:=20UserFormDto=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 회원가입 시에 필요한 Dto --- .../com/example/WebOrder/dto/UserFormDto.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/example/WebOrder/dto/UserFormDto.java diff --git a/src/main/java/com/example/WebOrder/dto/UserFormDto.java b/src/main/java/com/example/WebOrder/dto/UserFormDto.java new file mode 100644 index 0000000000..927fb5b3a7 --- /dev/null +++ b/src/main/java/com/example/WebOrder/dto/UserFormDto.java @@ -0,0 +1,23 @@ +package com.example.WebOrder.dto; + +import jakarta.validation.constraints.NotEmpty; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.validator.constraints.Length; + +@Getter +@Setter +public class UserFormDto { + @NotEmpty(message = "아이디는 필수 입력 값입니다.") + private String username; + + @NotEmpty(message = "비밀번호는 필수 입력 값입니다.") + @Length(min = 8, max = 16, message = "비밀번호는 8자 이상, 16자 이하로 입력해주세요.") + private String password; + + @NotEmpty(message = "비밀번호를 한 번 더 입력해주세요.") + private String passwordCheck; + + @NotEmpty(message = "이름은 필수 입력 값입니다.") + private String name; +} From 623f2809dbc31223dbd0aae1bcea062bdaf2fbbe Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:58:16 +0900 Subject: [PATCH 11/13] =?UTF-8?q?:lipstick:=20assets:=20loginForm.html=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그인 화면 구현 - thymeleaf를 사용하여 error 필드 활용 --- .../resources/templates/html/loginForm.html | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/resources/templates/html/loginForm.html diff --git a/src/main/resources/templates/html/loginForm.html b/src/main/resources/templates/html/loginForm.html new file mode 100644 index 0000000000..f30648cb3f --- /dev/null +++ b/src/main/resources/templates/html/loginForm.html @@ -0,0 +1,36 @@ + + + + + + + + + +
+ + + + +
+ 계정이 없으신가요?     + 회원가입 +
+ +
+ + From 407a728cf5d454166b7952c1c2c9fe7cdf448e12 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 22:58:40 +0900 Subject: [PATCH 12/13] =?UTF-8?q?:lipstick:=20assets:=20loginForm.css=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - loginForm.html을 위한 css 파일 --- src/main/resources/static/css/loginForm.css | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/main/resources/static/css/loginForm.css diff --git a/src/main/resources/static/css/loginForm.css b/src/main/resources/static/css/loginForm.css new file mode 100644 index 0000000000..5e4965c99b --- /dev/null +++ b/src/main/resources/static/css/loginForm.css @@ -0,0 +1,82 @@ +.body { + overflow:hidden; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width:100vw; + height:100vh; +} +.logo { + background-color:rgba(157.25000202655792, 157.25000202655792, 157.25000202655792, 1); + width:130px; + height:130px; + margin-bottom: 20px; +} +.login-form { + width:320px; + height:auto; + position:relative; + margin-top: 20px; +} +.form-group { + width:320px; + height:79px; + position:relative; + margin-top:15px; +} +.form-label { + color:rgba(64.00000378489494, 64.00000378489494, 64.00000378489494, 1); + position:relative; + font-family:Noto Sans KR, sans-serif; + text-align:left; + font-size:16px; + letter-spacing:0; +} +.form-input-text { + background-color: rgba(255, 255, 255, 1); + width: 320px; + height: 50px; + position: relative; + font-family: Noto Sans KR, sans-serif; + text-align: left; + font-size: 14px; + border-radius: 5px; + border: 1px solid rgba(174.00000482797623, 174.00000482797623, 174.00000482797623, 1); +} + +.login-button { + background-color:rgba(91.46796986460686, 96.86175003647804, 226.31249696016312, 1); + width:320px; + height:55px; + color:rgba(255, 255, 255, 1); + font-family:Noto Sans KR, sans-serif; + text-align:center; + font-size:16px; + letter-spacing:0; + position:relative; + margin-top: 20%; +} +.global-error { + border-color: #dc3545; + color: #dc3545; + font-family:Noto Sans KR, sans-serif; + font-size:12px; + letter-spacing:0; +} +.ask-register { + color:rgba(102.00000151991844, 102.00000151991844, 102.00000151991844, 1); + font-family:Noto Sans KR, sans-serif; + font-size:16px; + letter-spacing:0; + display: flex; + align-items: center; + position:relative; + margin-top: 10%; +} \ No newline at end of file From 55119b7004fb0043d2793bf3ffe4e5ea9a511ff4 Mon Sep 17 00:00:00 2001 From: hightuv Date: Sun, 24 Mar 2024 23:02:07 +0900 Subject: [PATCH 13/13] =?UTF-8?q?:sparkles:=20feat:=20LoginController?= =?UTF-8?q?=EC=97=90=20=EB=A1=9C=EA=B7=B8=EC=9D=B8,=20=ED=9A=8C=EC=9B=90?= =?UTF-8?q?=EA=B0=80=EC=9E=85=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그인, 회원가입 로직 추가 - BindingResult를 사용하여 Error 필드 활용 - 오류가 있을 시, thymeleaf와의 연계로 뷰에 에러 메시지를 출력할 수 있음 - 특정 로직 수행 시 어떤 화면으로 가야할 지 지정해둠 - 조금 번잡해서 추후 리팩토링 필요할 수 있음 --- .../WebOrder/controller/LoginController.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/WebOrder/controller/LoginController.java b/src/main/java/com/example/WebOrder/controller/LoginController.java index 237cff0720..453b3892fd 100644 --- a/src/main/java/com/example/WebOrder/controller/LoginController.java +++ b/src/main/java/com/example/WebOrder/controller/LoginController.java @@ -1,20 +1,28 @@ 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") @@ -22,41 +30,56 @@ 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:/"; } } }