-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#12 이메일 찾기 SMS 인증 구현 #16
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
24c0eec
:heavy_plus_sign: chore: coolSMS 관련 의존성 추가 및 환경변수 설정
jinnieusLab 775a1d8
:sparkles: feat: 휴대폰 인증 요청, 응답 구조 구현
jinnieusLab 07e58a7
:sparkles: feat: 휴대폰 인증 요청 에러 처리
jinnieusLab 6f968ee
:sparkles: feat: 휴대폰 인증 구현
jinnieusLab 65fb560
:recycle: refactor: sms 전송 실패 에러 처리 추가
jinnieusLab bd3f08c
:recycle: refactor: sms 인증 후 응답에 이메일 포함하도록 변경
jinnieusLab 4afbd2b
:memo: docs: SMS 인증 관련 Swagger API 명세서 수정
jinnieusLab 1c614ba
:recycle: refactor: 문자 전송 전 유저 가입 여부 로직 추가
jinnieusLab 478caf3
:wrench: chore: ci 수정 및 에러 해결
jinnieusLab 99d0766
:wrench: chore: 에러 코드 변경
jinnieusLab 7af6ece
:twisted_rightwards_arrows: chore: merge from develop, 로그 메시지 수정
jinnieusLab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/whereyouad/WhereYouAd/domains/user/application/dto/request/SmsRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Pattern; | ||
|
|
||
| public class SmsRequest { | ||
| public record SmsSendRequest( | ||
| @NotBlank(message = "전화번호는 필수입니다.") | ||
| @Pattern(regexp = "^\\d{10,11}$", message = "전화번호는 하이픈 없이 10~11자리 숫자만 입력해주세요.") String phoneNumber) { | ||
| } | ||
|
|
||
| public record SmsVerifyRequest( | ||
| @NotBlank(message = "전화번호는 필수입니다.") String phoneNumber, | ||
| @NotBlank(message = "인증코드는 필수입니다.") String verificationCode) { | ||
| } | ||
|
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 검증 요청에도 전화번호/인증코드 형식 검증을 맞춰주세요 발송 요청은 10~11자리만 허용하지만 검증 요청은 제약이 없어 ✅ 수정 제안 public record SmsVerifyRequest(
- `@NotBlank`(message = "전화번호는 필수입니다.") String phoneNumber,
- `@NotBlank`(message = "인증코드는 필수입니다.") String verificationCode) {
+ `@NotBlank`(message = "전화번호는 필수입니다.")
+ `@Pattern`(regexp = "^\\d{10,11}$", message = "전화번호는 하이픈 없이 10~11자리 숫자만 입력해주세요.") String phoneNumber,
+ `@NotBlank`(message = "인증코드는 필수입니다.")
+ `@Pattern`(regexp = "^\\d{6}$", message = "인증코드는 6자리 숫자만 입력해주세요.") String verificationCode) {
}🤖 Prompt for AI Agents |
||
| } | ||
16 changes: 16 additions & 0 deletions
16
...ain/java/com/whereyouad/WhereYouAd/domains/user/application/dto/response/SmsResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.response; | ||
|
|
||
| public class SmsResponse { | ||
|
|
||
| public record SmsSentResponse( | ||
| String message, | ||
| String phoneNumber, // 전송한 휴대폰 번호 | ||
| long expireIn) { | ||
| } | ||
|
|
||
| public record SmsVerifiedResponse( | ||
| boolean isVerified, | ||
| String verificationMessage, | ||
| String email) { | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/whereyouad/WhereYouAd/domains/user/domain/service/SmsService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.domain.service; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.user.exception.code.UserErrorCode; | ||
| import com.whereyouad.WhereYouAd.domains.user.exception.handler.UserHandler; | ||
| import com.whereyouad.WhereYouAd.domains.user.persistence.entity.User; | ||
| import com.whereyouad.WhereYouAd.domains.user.persistence.repository.UserRepository; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.nurigo.sdk.NurigoApp; | ||
| import net.nurigo.sdk.message.model.Message; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.SmsResponse; | ||
| import com.whereyouad.WhereYouAd.global.utils.RedisUtil; | ||
| import net.nurigo.sdk.message.request.SingleMessageSendingRequest; | ||
| import net.nurigo.sdk.message.service.DefaultMessageService; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| @Service | ||
| @Transactional | ||
| @Slf4j | ||
| public class SmsService { | ||
| private final RedisUtil redisUtil; | ||
| private final UserRepository userRepository; | ||
| private final DefaultMessageService defaultMessageService; | ||
| private final String senderNumber; | ||
|
|
||
| public SmsService(RedisUtil redisUtil, UserRepository userRepository, | ||
| @Value("${coolSms.apiKey}") String smsApiKey, | ||
| @Value("${coolSms.secretKey}") String smsSecretKey, | ||
| @Value("${coolSms.senderNumber}") String senderNumber) { | ||
| this.redisUtil = redisUtil; | ||
| this.userRepository = userRepository; | ||
| this.senderNumber = senderNumber; | ||
| this.defaultMessageService = NurigoApp.INSTANCE.initialize(smsApiKey, smsSecretKey, | ||
| "https://api.coolsms.co.kr"); | ||
| } | ||
|
|
||
| // SMS 전송 | ||
| public SmsResponse.SmsSentResponse sendSms(String phoneNumber) { | ||
| // 이메일 찾기 기능: 가입된 유저인지 확인 | ||
| if (!userRepository.existsByPhoneNumber(phoneNumber)) { | ||
| throw new UserHandler(UserErrorCode.USER_NOT_FOUND_BY_PHONE); | ||
| } | ||
|
|
||
| Message message = new Message(); | ||
| String verificationCode = generateCode(); | ||
|
|
||
| message.setFrom(senderNumber); // 발신 번호 | ||
| message.setTo(phoneNumber); | ||
| message.setText("[Where You Ad] 이메일 찾기\n 휴대폰 인증 번호는 [" + verificationCode + "] 입니다."); | ||
|
|
||
| // redis 저장 | ||
| redisUtil.setDataExpire(phoneNumber, verificationCode, 180); // 유효 기간 3분 | ||
|
|
||
| try { | ||
| this.defaultMessageService.sendOne(new SingleMessageSendingRequest(message)); | ||
| } catch (Exception e) { | ||
| log.error(e.getMessage()); | ||
| throw new UserHandler(UserErrorCode.SMS_SEND_FAILED); | ||
| } | ||
|
|
||
| return new SmsResponse.SmsSentResponse("인증번호가 전송되었습니다.", phoneNumber, 180L); | ||
| } | ||
|
|
||
| // 랜덤 인증 번호 생성 | ||
| public String generateCode() { | ||
| Random random = new Random(); | ||
| int code = 100000 + random.nextInt(900000); // 6자리 인증 번호 | ||
| return Integer.toString(code); | ||
| } | ||
|
|
||
| // 인증번호 확인 | ||
| public boolean verifyCode(String phoneNumber, String insertedNum) { | ||
| String savedCode = redisUtil.getData(phoneNumber); | ||
|
|
||
| if (savedCode != null && savedCode.equals(insertedNum)) { | ||
| redisUtil.deleteData(phoneNumber); | ||
| // 인증 완료 | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // 인증번호 확인 후 유저 이메일 반환 | ||
| public String isPhoneVerified(String phoneNumber, String insertedNum) { | ||
| if (verifyCode(phoneNumber, insertedNum)) { | ||
| User user = userRepository.findUserByPhoneNumber(phoneNumber) | ||
| .orElseThrow(() -> new UserHandler(UserErrorCode.USER_NOT_FOUND_BY_PHONE)); | ||
| return user.getEmail(); | ||
| } else | ||
| throw new UserHandler(UserErrorCode.USER_SMS_NOT_VERIFIED); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.