Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 이메일 인증 예외처리 개선 #242

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
.DS_store
.DS_Store

### STS ###
.apt_generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ enum class ErrorCode(val code: String, val message: String, var status: Int) {
HttpStatus.TOO_MANY_REQUESTS.value()
),
EMAIL_SEND_FAILED("E06", "Failed to send email.", HttpStatus.INTERNAL_SERVER_ERROR.value()),
EMAIL_VERIFICATION_CODE_EXPIRED("E06", "인증 코드가 만료되었습니다.", HttpStatus.BAD_REQUEST.value()),

// JWT
JWT_TOKEN_NOT_FOUND("J001", "Token not found", HttpStatus.UNAUTHORIZED.value()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,13 @@ class UserService(
}
return TeamBranch.JOINED
}

@Transactional
fun getOrCreateUser(email: String): User {
return try {
getUserByEmail(email)
} catch (e: UserNotFoundException) {
createUserByEmail(email)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,47 +140,7 @@ class VerificationApi(
)]
)]
),
ApiResponse(
responseCode = "401",
description = "인증 오류",
content =
[
Content(
schema = Schema(implementation = ErrorResponse::class),
examples =
[
ExampleObject(
name = "Token Not Found",
value =
"{\"message\": \"Token not found\", \"status\": 401, \"code\": \"J001\"}"
),
ExampleObject(
name = "Token Expired",
value =
"{\"message\": \"Token has expired\", \"status\": 401, \"code\": \"J002\"}"
),
ExampleObject(
name = "Invalid Token Format",
value =
"{\"message\": \"Invalid token format\", \"status\": 401, \"code\": \"J003\"}"
),
ExampleObject(
name = "Invalid Token Signature",
value =
"{\"message\": \"Invalid token signature\", \"status\": 401, \"code\": \"J004\"}"
),
ExampleObject(
name = "Refresh Token Not Found",
value =
"{\"message\": \"Refresh token not found\", \"status\": 401, \"code\": \"J005\"}"
),
ExampleObject(
name = "Refresh Token Expired",
value =
"{\"message\": \"Refresh token has expired\", \"status\": 401, \"code\": \"J006\"}"
)]
)]
)]
]
)
// TODO: Service layer로 이동해야함
@PostMapping("/verify-email")
Expand All @@ -192,12 +152,7 @@ class VerificationApi(
val requestInfo = requestUtils.toRequestInfoDto(request)
emailVerificationService.verifyEmail(body.email, body.code, requestInfo)

val user =
try {
userService.getUserByEmail(body.email)
} catch (e: Exception) {
userService.createUserByEmail(body.email)
}
val user = userService.getOrCreateUser(body.email)

val accessToken = authService.issueTokens(user.id!!, response)
return ResponseEntity.ok(accessToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uoslife.servermeeting.verification.exception

import uoslife.servermeeting.global.error.exception.BusinessException
import uoslife.servermeeting.global.error.exception.ErrorCode

class EmailVerificationCodeExpiredException :
BusinessException(ErrorCode.EMAIL_VERIFICATION_CODE_EXPIRED) {}
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ class EmailVerificationService(
validateVerificationAttempts(email)
incrementVerificationAttempts(email)
// Redis에서 인증 코드 조회
val redisCode = getVerificationCode(email)
val redisCode = getVerificationCode(email, requestInfo)
// 인증 코드 검증
validateVerificationCode(redisCode, code, email, requestInfo)
// 검증 성공한 코드 삭제
clearVerificationData(email)
}

private fun getVerificationCode(email: String): String {
private fun getVerificationCode(email: String, requestInfo: RequestInfoDto): String {
val verificationCodeKey =
VerificationUtils.generateRedisKey(VerificationConstants.CODE_PREFIX, email)
return redisTemplate.opsForValue().get(verificationCodeKey).toString()
val code = redisTemplate.opsForValue().get(verificationCodeKey)
if (code == null) {
logger.warn("[이메일 인증 실패(만료)] email: $email, $requestInfo")
throw EmailVerificationCodeExpiredException()
}
return code.toString()
}

private fun validateVerificationCode(
Expand All @@ -75,7 +80,7 @@ class EmailVerificationService(
requestInfo: RequestInfoDto
) {
if (redisCode != code) {
logger.warn("[이메일 인증 실패] email: $email, $requestInfo")
logger.warn("[이메일 인증 실패(불일치)] email: $email, $requestInfo")
throw EmailVerificationCodeMismatchException()
}
logger.info("[이메일 인증 성공] email: $email")
Expand Down
Loading