Skip to content

Commit a4d07b2

Browse files
[FEAT] 회원가입 시, 이메일 인증번호 로직 추가 (#145) (#146)
* [FIX] host와 비밀번호 뒤바뀐 것 수정 * [FEAT] 이메일로 인증번호 전송 * [FEAT] 이메일 인증번호 검증 로직 추가 * [FEAT] 로그인 시 이메일 인증 여부 확인 로직 추가 * [FEAT] 개발 서버tyml에 이메일 정보 추가 * [FEAT] 이메일 중복 확인 추가
1 parent 0ca17a5 commit a4d07b2

19 files changed

+279
-18
lines changed

src/main/java/com/brainpix/api/code/error/AuthorityErrorCode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
public enum AuthorityErrorCode implements ErrorCode {
1111
PASSWORD_NOT_MATCH(HttpStatus.BAD_REQUEST, "AUTHORITY400", "비밀번호가 일치하지 않습니다."),
1212
AUTHORITY_ERROR_CODE(HttpStatus.FORBIDDEN, "AUTHORITY403", "권한이 없습니다."),
13+
EMAIL_AUTH_CODE_NOT_FOUND(HttpStatus.BAD_REQUEST, "AUTHORITY400", "인증 코드를 먼저 요청해주세요."),
14+
EMAIL_AUTH_CODE_NOT_MATCH(HttpStatus.BAD_REQUEST, "AUTHORITY400", "인증 코드가 일치하지 않습니다."),
15+
EMAIL_AUTH_CODE_EXPIRED(HttpStatus.BAD_REQUEST, "AUTHORITY400", "인증 코드가 만료되었습니다."),
16+
EMAIL_NOT_MATCHED(HttpStatus.BAD_REQUEST, "AUTHORITY400", "인증받은 이메일이 아닙니다"),
17+
EMAIL_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "AUTHORITY400", "다른 이메일을 사용해주세요."),
1318
;
1419

1520
private final HttpStatus httpStatus;

src/main/java/com/brainpix/config/MailConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ public class MailConfig {
1515
private final String USERNAME;
1616
private final String PASSWORD;
1717

18-
public MailConfig(@Value("${spring.mail.password}") String HOST,
18+
public MailConfig(
19+
@Value("${spring.mail.host}") String HOST,
1920
@Value("${spring.mail.username}") String USERNAME,
20-
@Value("${spring.mail.host}") String PASSWORD) {
21+
@Value("${spring.mail.password}") String PASSWORD) {
2122
this.HOST = HOST;
2223
this.USERNAME = USERNAME;
2324
this.PASSWORD = PASSWORD;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.brainpix.security.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.brainpix.api.ApiResponse;
10+
import com.brainpix.security.dto.EmailAuthCode;
11+
import com.brainpix.security.dto.request.SendEmailNumberRequest;
12+
import com.brainpix.security.service.EmailAuthService;
13+
14+
import io.swagger.v3.oas.annotations.Operation;
15+
import io.swagger.v3.oas.annotations.tags.Tag;
16+
import lombok.RequiredArgsConstructor;
17+
18+
@Tag(name = "회원가입시 사용되는 이메일 인증 API", description = "회원가입시 사용되는 이메일 인증 API입니다.")
19+
@RestController
20+
@RequestMapping("/users/login/email")
21+
@RequiredArgsConstructor
22+
public class EmailController {
23+
private final EmailAuthService emailAuthService;
24+
25+
@Operation(summary = "입력 이메일로 인증 번호 전송", description = "입력한 이메일로 인증번호를 전송합니다.")
26+
@PostMapping
27+
public ResponseEntity<ApiResponse<Void>> postEmail(@RequestBody SendEmailNumberRequest sendEmailNumberRequest) {
28+
emailAuthService.sendEmailAuthCode(sendEmailNumberRequest);
29+
return ResponseEntity.ok(ApiResponse.successWithNoData());
30+
}
31+
32+
@Operation(summary = "인증번호 확인", description = "입력한 인증번호가 맞는지 확인합니다.")
33+
@PostMapping("/auth")
34+
public ResponseEntity<ApiResponse<EmailAuthCode.Response>> checkAuthCode(
35+
@RequestBody EmailAuthCode.Request request) {
36+
EmailAuthCode.Response response = emailAuthService.checkEmailAuthCode(request);
37+
return ResponseEntity.ok(ApiResponse.success(response));
38+
}
39+
}

src/main/java/com/brainpix/security/controller/SignInController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class SignInController {
3131
public ResponseEntity<ApiResponse<SignInResponse>> singIn(@RequestBody SignInRequest signInRequest) {
3232
Authentication authentication = authenticationManager.authenticate(
3333
SignInConverter.toAuthenticationToken(signInRequest));
34-
String jwt = tokenManager.writeToken(authentication);
34+
String jwt = tokenManager.writeAuthenticationToken(authentication);
3535
return ResponseEntity.ok(ApiResponse.success(new SignInResponse("Bearer " + jwt)));
3636
}
3737
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.brainpix.security.converter;
2+
3+
import com.brainpix.security.dto.EmailAuthCode;
4+
5+
public class EmailAuthCodeConverter {
6+
public static EmailAuthCode.Response toResponse(String token) {
7+
return EmailAuthCode.Response.builder()
8+
.token(token)
9+
.build();
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.brainpix.security.dto;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
public class EmailAuthCode {
8+
9+
@Getter
10+
@NoArgsConstructor
11+
public static class Request {
12+
private String email;
13+
private String authCode;
14+
}
15+
16+
@Getter
17+
@Builder
18+
public static class Response {
19+
private String token;
20+
}
21+
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.brainpix.security.dto.request;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
6+
@Getter
7+
@NoArgsConstructor
8+
public class SendEmailNumberRequest {
9+
private String email;
10+
}

src/main/java/com/brainpix/security/dto/request/SignUpRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public abstract static class CommonSignUpRequest {
1919
protected String name;
2020
protected LocalDate birthday;
2121
protected String email;
22+
protected String emailToken;
2223

2324
public abstract User toEntity(String encodedPassword);
2425

src/main/java/com/brainpix/security/filter/JwtAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3838
} else {
3939
jwt = parseJwt(jwt);
4040
try {
41-
BrainpixAuthenticationToken authenticationToken = tokenManager.readToken(jwt);
41+
BrainpixAuthenticationToken authenticationToken = tokenManager.readAuthenticationToken(jwt);
4242
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
4343
filterChain.doFilter(request, response);
4444
}

src/main/java/com/brainpix/security/service/CompanySignUpService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ public class CompanySignUpService extends SignUpService {
1414
private final ProfileRepository profileRepository;
1515

1616
public CompanySignUpService(UserRepository userRepository,
17-
PasswordEncoder passwordEncoder,
18-
ProfileRepository profileRepository) {
19-
super(userRepository, passwordEncoder);
17+
PasswordEncoder passwordEncoder, ProfileRepository profileRepository, EmailAuthService emailAuthService) {
18+
super(userRepository, passwordEncoder, emailAuthService);
2019
this.profileRepository = profileRepository;
2120
}
2221

0 commit comments

Comments
 (0)