Skip to content

Commit 0076c0c

Browse files
authored
Merge pull request #3 from sinsehwan/sinsehwan
add and modify : signup validation implement
2 parents 4b3a744 + 14c02d9 commit 0076c0c

File tree

7 files changed

+239
-11
lines changed

7 files changed

+239
-11
lines changed

src/main/java/apptive/devlog/auth/controller/UserController.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,30 @@ public String signupForm(Model model){
5454
*/
5555
@PostMapping("/signup")
5656
public String signup(@Validated @ModelAttribute("user") UserSaveForm form, BindingResult bindingResult){
57-
// 검증 처리
57+
// 이메일 중복 체크
58+
if(form.getEmail() != null){
59+
if(userService.isEmailDuplicated(form.getEmail())){
60+
bindingResult.rejectValue("email", "duplicated", "이미 가입된 이메일입니다.");
61+
}
62+
}
5863

64+
// 닉네임 중복 체크
65+
if(form.getNickname() != null){
66+
if(userService.isNicknameDuplicated(form.getNickname())){
67+
bindingResult.rejectValue("nickname", "duplicated", "이미 사용 중인 닉네임입니다.");
68+
}
69+
}
70+
71+
// Validation
72+
if(bindingResult.hasErrors()){
73+
log.info("errors = {}", bindingResult);
74+
75+
return "/users/home";
76+
}
5977

6078
// 성공 로직
79+
User user = new User(form);
80+
userService.signup(user);
6181

6282
return "redirect:/users/home";
6383
}
@@ -82,7 +102,16 @@ public String loginForm(Model model){
82102
*/
83103
@PostMapping("login")
84104
public String login(@Validated @ModelAttribute("user") UserLoginForm form, BindingResult bindingResult){
85-
// 검증 구현 필요
105+
// 1차 유효성 검사
106+
if(bindingResult.hasErrors()){
107+
log.info("errors = {}", bindingResult);
108+
109+
return "users/login";
110+
}
111+
112+
User user = new User(form);
113+
114+
//로그인 처리
86115

87116
return "redirect:/main";
88117
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
package apptive.devlog.auth.dto;
22

33

4+
import apptive.devlog.auth.entity.Gender;
5+
import jakarta.persistence.EnumType;
6+
import jakarta.persistence.Enumerated;
47
import jakarta.validation.constraints.Email;
58
import jakarta.validation.constraints.NotBlank;
69
import jakarta.validation.constraints.NotNull;
10+
import jakarta.validation.constraints.Pattern;
11+
import lombok.Data;
12+
import org.springframework.format.annotation.DateTimeFormat;
713

814
import java.time.LocalDate;
915

16+
@Data
1017
public class UserSaveForm {
11-
@NotNull
12-
@Email
18+
@NotNull(message = "이메일은 필수입니다.")
19+
@Email(message = "올바른 이메일 형식이 아닙니다.")
1320
private String email;
1421

15-
@NotNull
22+
@NotNull(message = "비밀번호는 필수입니다.")
23+
@Pattern(
24+
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\W).{10,}$",
25+
message = "10자 이상으로 구성하세요. 대소문자와 특수문자를 모두 포함해야 합니다."
26+
)
1627
private String password;
1728

18-
@NotBlank
29+
@NotBlank(message = "이름은 필수입니다.")
1930
private String name;
2031

21-
@NotBlank
32+
@NotBlank(message = "닉네임은 필수입니다.")
2233
private String nickname;
2334

24-
35+
@NotNull(message = "생년월일은 필수입니다.")
36+
@DateTimeFormat(pattern = "yyyy-MM-dd")
2537
private LocalDate birth;
2638

27-
private String gender;
39+
@NotNull(message = "성별은 필수입니다.")
40+
@Enumerated(EnumType.STRING)
41+
private Gender gender;
2842

2943
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package apptive.devlog.auth.entity;
2+
3+
public enum Gender {
4+
MALE("남"),
5+
FEMALE("여");
6+
7+
private final String label;
8+
9+
Gender(String label){
10+
this.label = label;
11+
}
12+
13+
public String getLabel(){
14+
return label;
15+
}
16+
}

src/main/java/apptive/devlog/auth/entity/User.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package apptive.devlog.auth.entity;
22

3+
import apptive.devlog.auth.dto.UserLoginForm;
4+
import apptive.devlog.auth.dto.UserSaveForm;
35
import jakarta.persistence.*;
46
import lombok.Getter;
57
import lombok.Setter;
@@ -30,11 +32,11 @@ public class User {
3032

3133
private LocalDate birth;
3234

33-
private String gender;
35+
private Gender gender;
3436

3537
public User() {}
3638

37-
public User(String email, String password, String name, String nickname, LocalDate birth, String gender){
39+
public User(String email, String password, String name, String nickname, LocalDate birth, Gender gender){
3840
this.email = email;
3941
this.password = password;
4042
this.name = name;
@@ -43,4 +45,18 @@ public User(String email, String password, String name, String nickname, LocalDa
4345
this.gender = gender;
4446
}
4547

48+
public User(UserLoginForm form){
49+
this.email = form.getEmail();
50+
this.password = form.getPassword();
51+
}
52+
53+
public User(UserSaveForm form){
54+
this.email = form.getEmail();
55+
this.password = form.getPassword();
56+
this.name = form.getName();
57+
this.nickname = form.getNickname();
58+
this.birth = form.getBirth();
59+
this.gender = form.getGender();
60+
}
61+
4662
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
package apptive.devlog.auth.service;
22

3+
import apptive.devlog.auth.entity.User;
4+
import apptive.devlog.auth.repository.UserRepository;
5+
import lombok.RequiredArgsConstructor;
36
import org.springframework.stereotype.Service;
47

58
@Service
9+
@RequiredArgsConstructor
610
public class UserService {
711

12+
private final UserRepository userRepository;
813

14+
public User signup(User user){
15+
return userRepository.save(user);
16+
}
17+
18+
public boolean isEmailDuplicated(String email){
19+
// 중복 체크
20+
21+
return false;
22+
}
23+
24+
public boolean isNicknameDuplicated(String nickname){
25+
// 중복 체크
26+
27+
return false;
28+
}
929
}

src/main/resources/templates/users/login.html

+18
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,25 @@ <h2>로그인</h2>
2525
<input type="email" id="email" th:field="*{email}" class="form-control" placeholder="이메일 입력">
2626
</div>
2727

28+
<!-- 비밀번호 -->
29+
<div>
30+
<label for="password">비밀번호</label>
31+
<input type="password" id="password" th:field="*{password}" class="form-control" placeholder="비밀번호 입력">
32+
</div>
2833

34+
<hr class="my-4">
35+
36+
<div class="row">
37+
<div class="col">
38+
<button class="w-100 btn btn-primary btn-lg" type="submit">회원가입</button>
39+
</div>
40+
<div class="col">
41+
<button class="w-100 btn btn-secondary btn-lg"
42+
onclick="location.href='home.html'"
43+
th:onclick="|location.href='@{/users/home}'|"
44+
type="button">취소</button>
45+
</div>
46+
</div>
2947
</form>
3048
</div>
3149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<link th:href="@{/css/bootstrap.min.css}"
6+
href="../../static/css/bootstrap.min.css" rel="stylesheet">
7+
<title>Title</title>
8+
<style>
9+
.container {
10+
max-width: 560px;
11+
}
12+
.field-error {
13+
border-color: #dc3545;
14+
color: #dc3545
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
20+
<div class="container">
21+
<div class="py-5 text-center">
22+
<h2>회원가입</h2>
23+
</div>
24+
25+
<form action="/users/signup" th:object="${user}" method="post">
26+
<!-- 이메일 -->
27+
<div>
28+
<label for="email">이메일</label>
29+
<input type="email" id="email" th:field="*{email}"
30+
th:errorclass="field-error"
31+
class="form-control" placeholder="이메일 입력">
32+
<div class="field-error" th:erros="*{email}">
33+
이메일 오류
34+
</div>
35+
</div>
36+
37+
<!-- 비밀번호 -->
38+
<div>
39+
<label for="password">비밀번호</label>
40+
<input type="password" id="password" th:field="*{password}"
41+
th:errorclass="field-error"
42+
class="form-control" placeholder="비밀번호 입력">
43+
<div class="field-error" th:erros="*{password}">
44+
비밀번호 오류
45+
</div>
46+
</div>
47+
48+
<!-- 이름 -->
49+
<div>
50+
<label for="name">이름</label>
51+
<input type="text" id="name" th:field="${name}"
52+
th:errorclass="field-error"
53+
class="form-control" placeholder="이름 입력">
54+
<div class="field-error" th:erros="*{name}">
55+
이름 오류
56+
</div>
57+
</div>
58+
59+
<!-- 닉네임 -->
60+
<div>
61+
<label for="nickname">닉네임</label>
62+
<input type="text" id="nickname" th:field="${nickname}"
63+
th:errorclass="field-error"
64+
class="form-control" placeholder="닉네임 입력">
65+
<div class="field-error" th:erros="*{nickname}">
66+
닉네임 오류
67+
</div>
68+
</div>
69+
70+
<!-- 생년월일 -->
71+
<div>
72+
<label for="birth">생년월일</label>
73+
<input type="date" id="birth" th:field="${birth}"
74+
th:errorclass="field-error"
75+
class="form-control">
76+
<div class="field-error" th:erros="*{birth}">
77+
생년월일 오류
78+
</div>
79+
</div>
80+
81+
<!-- 성별 -->
82+
<div>
83+
<label>성별</label><br>
84+
<input type="radio" id="male" name="gender"
85+
th:errorclass="field-error"
86+
th:field="*{gender}" value="male">
87+
<label for="male"></label>
88+
89+
<input type="radio" id="female" name="gender"
90+
th:errorclass="field-error"
91+
th:field="*{gender}" value="female">
92+
<label for="female"></label>
93+
<div class="field-error" th:erros="*{gender}">
94+
성별 오류
95+
</div>
96+
</div>
97+
98+
<hr class="my-4">
99+
100+
<div class="row">
101+
<div class="col">
102+
<button class="w-100 btn btn-primary btn-lg" type="submit">회원가입</button>
103+
</div>
104+
<div class="col">
105+
<button class="w-100 btn btn-secondary btn-lg"
106+
onclick="location.href='home.html'"
107+
th:onclick="|location.href='@{/users/home}'|"
108+
type="button">취소</button>
109+
</div>
110+
</div>
111+
</form>
112+
</div>
113+
114+
</body>
115+
</html>

0 commit comments

Comments
 (0)