Skip to content

Commit

Permalink
refactor: 아이디, 비밀번호 찾기 로직 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
dgjinsu committed Jan 14, 2025
1 parent e5c2fd6 commit 4912f05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
56 changes: 25 additions & 31 deletions src/main/java/jikgong/domain/member/service/MemberInfoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class MemberInfoService {
private final StayExpirationService stayExpirationService;

private static final String REDIS_PREFIX_PASSWORD_RESET = "password_reset:";
private static final String REDIS_PREFIX_LOGIN_ID_FIND = "loginId_find:";

/**
* 노동자: 회원 정보 조회
Expand Down Expand Up @@ -79,7 +80,8 @@ public void updateWorkerInfo(Long workerId, WorkerInfoRequest request) {
/**
* 경력 정보 업데이트 로직
*/
private void updateWorkExperiences(List<WorkExperienceRequest> workExperienceRequestList, Member worker) {
private void updateWorkExperiences(List<WorkExperienceRequest> workExperienceRequestList,
Member worker) {
List<WorkExperience> currentWorkExperiences = worker.getWorkExperienceList();
List<WorkExperience> newWorkExperiences = new ArrayList<>();

Expand All @@ -103,7 +105,8 @@ private void updateWorkExperiences(List<WorkExperienceRequest> workExperienceReq
.map(WorkExperienceRequest::getWorkExperienceId)
.filter(Objects::nonNull) // null 값 필터링
.collect(Collectors.toList());
workExperienceRepository.deleteWorkExperienceNotInIdList(worker.getId(), workExperienceIdList);
workExperienceRepository.deleteWorkExperienceNotInIdList(worker.getId(),
workExperienceIdList);

// 경력 추가
workExperienceRepository.saveAll(newWorkExperiences);
Expand Down Expand Up @@ -158,33 +161,26 @@ public List<CompanySearchResponse> searchCompany(String keyword) {
* 비밀번호 찾기 전 문자 인증
*/
public void verificationBeforeFindPassword(PasswordFindRequest request) throws Exception {
Member member = memberRepository.findMemberForForgottenPassword(request.getLoginId(), request.getPhone())
Member member = memberRepository.findMemberForForgottenPassword(request.getLoginId(),
request.getPhone())
.orElseThrow(() -> new JikgongException(ErrorCode.MEMBER_NOT_FOUND));

// 6자리 랜덤 코드 생성
String authCode = RandomCode.createAuthCode();
String content = "[직공]\n비밀번호 찾기 본인확인 인증번호: [" + authCode + "]";
try {
smsService.sendSms(member.getPhone(), content);
} catch (Exception e) {
throw new JikgongException(ErrorCode.SMS_SEND_FAIL);
}
smsService.sendSms(member.getPhone(), content);

// Redis에 인증 코드와 회원 정보를 저장 (TTL 5분)
redisTemplate.opsForValue().set(member.getPhone(), authCode, 5, TimeUnit.MINUTES);
String redisKey = REDIS_PREFIX_PASSWORD_RESET + member.getPhone();
redisTemplate.opsForValue().set(redisKey, authCode, 5, TimeUnit.MINUTES);
}

/**
* 임시 비밀번호 발급
*/
public PasswordFindResponse updateTemporaryPassword(AuthCodeForFindRequest request) {
// Redis에 저장된 인증 코드 가져오기
String savedAuthCode = redisTemplate.opsForValue().get(request.getPhone());

// 인증 코드가 일치하는지 체크
if (savedAuthCode == null || !savedAuthCode.equals(request.getAuthCode())) {
throw new JikgongException(ErrorCode.MEMBER_INVALID_AUTH_CODE); // 인증 코드 불일치
}
// 인증 코드 검증
validateAuthCode(request, REDIS_PREFIX_PASSWORD_RESET);

// 임시 번호 생성, 업데이트 및 반환
String temporaryPassword = RandomCode.createTemporaryPassword();
Expand All @@ -199,20 +195,17 @@ public PasswordFindResponse updateTemporaryPassword(AuthCodeForFindRequest reque
* 아이디 찾기 전 문자 인증
*/
public void verificationBeforeFindLoginId(LoginIdFindRequest request) {
Member member = memberRepository.findMemberForForgottenLoginId(request.getPhone(), request.getName())
Member member = memberRepository.findMemberForForgottenLoginId(request.getPhone(),
request.getName())
.orElseThrow(() -> new JikgongException(ErrorCode.MEMBER_NOT_FOUND));

// 6자리 랜덤 코드 생성
String authCode = RandomCode.createAuthCode();
String content = "[직공]\n아이디 찾기 본인확인 인증번호: [" + authCode + "]";
try {
smsService.sendSms(member.getPhone(), content);
} catch (Exception e) {
throw new JikgongException(ErrorCode.SMS_SEND_FAIL);
}
smsService.sendSms(member.getPhone(), content);

// Redis에 인증 코드와 회원 정보를 저장 (TTL 5분)
String redisKey = REDIS_PREFIX_PASSWORD_RESET + member.getPhone();
String redisKey = REDIS_PREFIX_LOGIN_ID_FIND + member.getPhone();
redisTemplate.opsForValue().set(redisKey, authCode, 5, TimeUnit.MINUTES);
}

Expand All @@ -221,7 +214,7 @@ public void verificationBeforeFindLoginId(LoginIdFindRequest request) {
*/
public LoginIdFindResponse findLoginId(AuthCodeForFindRequest request) {
// 인증 코드가 일치하는지 체크
validationAuthCode(request);
validateAuthCode(request, REDIS_PREFIX_LOGIN_ID_FIND);

Member member = memberRepository.findByPhone(request.getPhone())
.orElseThrow(() -> new JikgongException(ErrorCode.MEMBER_NOT_FOUND));
Expand All @@ -230,24 +223,25 @@ public LoginIdFindResponse findLoginId(AuthCodeForFindRequest request) {
return LoginIdFindResponse.from(member);
}

// 인증 코드가 일치하는지 체크
private void validationAuthCode(AuthCodeForFindRequest request) {
private void validateAuthCode(AuthCodeForFindRequest request, String prefix) {
// Redis에 저장된 인증 코드 가져오기
String redisKey = REDIS_PREFIX_PASSWORD_RESET + request.getPhone();
String redisKey = prefix + request.getPhone();
String savedAuthCode = redisTemplate.opsForValue().get(redisKey);

if (savedAuthCode == null || !savedAuthCode.equals(request.getAuthCode())) {
throw new JikgongException(ErrorCode.MEMBER_INVALID_AUTH_CODE); // 인증 코드 불일치
}
}


/**
* 체류 만료일 조회 api 호출
* 체류 만료일 정보 업데이트
* 체류 만료일 조회 api 호출 체류 만료일 정보 업데이트
*/
public void updateVisaExpiryDate(Long workerId, StayExpirationRequest request) throws JsonProcessingException {
public void updateVisaExpiryDate(Long workerId, StayExpirationRequest request)
throws JsonProcessingException {
// codef api 호출
StayExpirationResponse stayExpirationResponse = stayExpirationService.checkStayExpiration(request);
StayExpirationResponse stayExpirationResponse = stayExpirationService.checkStayExpiration(
request);

if (!"CF-00000".equals(stayExpirationResponse.getResult().getCode())) {
throw new JikgongException(ErrorCode.CODEF_UNKNOWN_ERROR);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/jikgong/global/sms/SmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ public class SmsService {
* sms 발송
*/
@Async
public void sendSms(String phoneNumber, String content) throws Exception {
public void sendSms(String phoneNumber, String content) {
Long time = System.currentTimeMillis();
String timestamp = time.toString();
String signature = makeSignature(timestamp);
String signature = null;
try {
signature = makeSignature(timestamp);
} catch (Exception e) {
throw new JikgongException(ErrorCode.SMS_SEND_FAIL);
}

SmsRequest.Message message = new SmsRequest.Message(phoneNumber, null, null);

Expand Down

0 comments on commit 4912f05

Please sign in to comment.