Skip to content

Commit

Permalink
Merge pull request #233 from PawWithU/feat/232-find-password-api-modify
Browse files Browse the repository at this point in the history
[Feature] 봉사자, 모집자 비밀번호 찾기 - 이메일 인증 API 수정
  • Loading branch information
kyeong-hyeok authored Jun 4, 2024
2 parents e707454 + cff4250 commit c092b6b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.pawwithu.connectdog.domain.auth.controller;

import com.pawwithu.connectdog.domain.auth.dto.request.EmailRequest;
import com.pawwithu.connectdog.domain.auth.dto.request.IntermediaryPhoneRequest;
import com.pawwithu.connectdog.domain.auth.dto.request.RefreshTokenRequest;
import com.pawwithu.connectdog.domain.auth.dto.request.VolunteerPhoneRequest;
import com.pawwithu.connectdog.domain.auth.dto.response.IntermediaryEmailResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.IntermediaryEmailWithAuthResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.VolunteerEmailResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.VolunteerEmailWithAuthResponse;
import com.pawwithu.connectdog.domain.auth.service.AuthService;
import com.pawwithu.connectdog.domain.auth.service.EmailService;
import com.pawwithu.connectdog.domain.oauth.dto.response.LoginResponse;
import com.pawwithu.connectdog.error.dto.ErrorResponse;
import com.pawwithu.connectdog.jwt.service.JwtService;
Expand Down Expand Up @@ -29,6 +37,7 @@ public class AuthController {

private final JwtService jwtService;
private final AuthService authService;
private final EmailService emailService;

@Operation(summary = "토큰 재발행", description = "AccessToken, RefreshToken 재발행 합니다.",
responses = {@ApiResponse(responseCode = "200", description = "토큰 재발행 성공")
Expand Down Expand Up @@ -96,4 +105,54 @@ public ResponseEntity<Void> intermediariesWithdraw(HttpServletRequest request, @
return ResponseEntity.noContent().build();
}

@Operation(summary = "이메일 찾기 - 봉사자 휴대폰 번호로 이메일 찾기", description = "봉사자 휴대폰 번호로 이메일을 찾습니다.",
responses = {@ApiResponse(responseCode = "200", description = "봉사자 휴대폰 번호로 이메일 찾기 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 휴대폰 번호는 필수 입력 값입니다. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/volunteers/search/email")
public ResponseEntity<VolunteerEmailResponse> findVolunteerEmail(@RequestBody @Valid VolunteerPhoneRequest request) {
VolunteerEmailResponse response = authService.findVolunteerEmail(request);
return ResponseEntity.ok(response);
}

@Operation(summary = "이메일 찾기 - 모집자 휴대폰 번호로 이메일 찾기", description = "모집자 휴대폰 번호로 이메일을 찾습니다.",
responses = {@ApiResponse(responseCode = "200", description = "모집자 휴대폰 번호로 이메일 찾기 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 휴대폰 번호는 필수 입력 값입니다. \t\n M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/intermediaries/search/email")
public ResponseEntity<IntermediaryEmailResponse> findIntermediaryEmail(@RequestBody @Valid IntermediaryPhoneRequest request) {
IntermediaryEmailResponse response = authService.findIntermediaryEmail(request);
return ResponseEntity.ok(response);
}

@Operation(summary = "비밀번호 찾기 - 이메일 인증번호 전송", description = "입력한 이메일로 인증번호를 전송합니다.",
responses = {@ApiResponse(responseCode = "200", description = "이메일 인증번호 전송 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 이메일 형식에 맞지 않습니다. \t\n V1, 이메일은 필수 입력 값입니다. \t\n A1, 이미 존재하는 이메일입니다. \t\n " +
"A4, 이메일 인증 코드 전송을 실패했습니다. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/volunteers/search/send-email")
public ResponseEntity<VolunteerEmailWithAuthResponse> sendEmailToVolunteer(@RequestBody @Valid EmailRequest request){
VolunteerEmailWithAuthResponse response = emailService.sendEmailToVolunteerWithoutAuth(request);
return ResponseEntity.ok(response);
}

@Operation(summary = "비밀번호 찾기 - 이메일 인증번호 전송", description = "입력한 이메일로 인증번호를 전송합니다.",
responses = {@ApiResponse(responseCode = "200", description = "이메일 인증번호 전송 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 이메일 형식에 맞지 않습니다. \t\n V1, 이메일은 필수 입력 값입니다. \t\n A1, 이미 존재하는 이메일입니다. \t\n " +
"A4, 이메일 인증 코드 전송을 실패했습니다. \t\n M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/intermediaries/search/send-email")
public ResponseEntity<IntermediaryEmailWithAuthResponse> sendEmailToIntermediary(@RequestBody @Valid EmailRequest request){
IntermediaryEmailWithAuthResponse response = emailService.sendEmailToIntermediaryWithoutAuth(request);
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.pawwithu.connectdog.domain.auth.controller;

import com.pawwithu.connectdog.domain.auth.dto.request.*;
import com.pawwithu.connectdog.domain.auth.dto.response.*;
import com.pawwithu.connectdog.domain.auth.dto.response.EmailResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.IntermediaryNameResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.IntermediaryPhoneResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.VolunteerPhoneResponse;
import com.pawwithu.connectdog.domain.auth.service.AuthService;
import com.pawwithu.connectdog.domain.auth.service.EmailService;
import com.pawwithu.connectdog.error.dto.ErrorResponse;
Expand Down Expand Up @@ -120,39 +123,4 @@ public ResponseEntity<IntermediaryNameResponse> isIntermediaryNameDuplicated(@Re
return ResponseEntity.ok(response);
}

@Operation(summary = "이메일 찾기 - 봉사자 휴대폰 번호로 이메일 찾기", description = "봉사자 휴대폰 번호로 이메일을 찾습니다.",
responses = {@ApiResponse(responseCode = "200", description = "봉사자 휴대폰 번호로 이메일 찾기 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 휴대폰 번호는 필수 입력 값입니다. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/volunteers/search/email")
public ResponseEntity<VolunteerEmailResponse> findVolunteerEmail(@RequestBody @Valid VolunteerPhoneRequest request) {
VolunteerEmailResponse response = authService.findVolunteerEmail(request);
return ResponseEntity.ok(response);
}

@Operation(summary = "이메일 찾기 - 모집자 휴대폰 번호로 이메일 찾기", description = "모집자 휴대폰 번호로 이메일을 찾습니다.",
responses = {@ApiResponse(responseCode = "200", description = "모집자 휴대폰 번호로 이메일 찾기 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 휴대폰 번호는 필수 입력 값입니다. \t\n M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/intermediaries/search/email")
public ResponseEntity<IntermediaryEmailResponse> findIntermediaryEmail(@RequestBody @Valid IntermediaryPhoneRequest request) {
IntermediaryEmailResponse response = authService.findIntermediaryEmail(request);
return ResponseEntity.ok(response);
}

@Operation(summary = "비밀번호 찾기 - 이메일 인증번호 전송", description = "입력한 이메일로 인증번호를 전송합니다.",
responses = {@ApiResponse(responseCode = "200", description = "이메일 인증번호 전송 성공")
, @ApiResponse(responseCode = "400"
, description = "V1, 이메일 형식에 맞지 않습니다. \t\n V1, 이메일은 필수 입력 값입니다. \t\n A1, 이미 존재하는 이메일입니다. \t\n A4, 이메일 인증 코드 전송을 실패했습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping(value = {"/volunteers/search/send-email", "/intermediaries/search/send-email"})
public ResponseEntity<EmailResponse> sendEmail(@RequestBody @Valid EmailRequest request){
EmailResponse emailResponse = emailService.sendEmailWithoutAuth(request);
return ResponseEntity.ok(emailResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pawwithu.connectdog.domain.auth.dto.response;

public record IntermediaryEmailWithAuthResponse(String authCode, String accessToken) {
public static IntermediaryEmailWithAuthResponse of(String authCode, String accessToken){
return new IntermediaryEmailWithAuthResponse(authCode, accessToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pawwithu.connectdog.domain.auth.dto.response;

public record VolunteerEmailWithAuthResponse(String authCode, String accessToken) {
public static VolunteerEmailWithAuthResponse of(String authCode, String accessToken){
return new VolunteerEmailWithAuthResponse(authCode, accessToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.pawwithu.connectdog.domain.auth.dto.request.EmailRequest;
import com.pawwithu.connectdog.domain.auth.dto.response.EmailResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.IntermediaryEmailWithAuthResponse;
import com.pawwithu.connectdog.domain.auth.dto.response.VolunteerEmailWithAuthResponse;
import com.pawwithu.connectdog.domain.intermediary.entity.Intermediary;
import com.pawwithu.connectdog.domain.intermediary.repository.IntermediaryRepository;
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository;
import com.pawwithu.connectdog.error.exception.custom.BadRequestException;
import com.pawwithu.connectdog.jwt.service.JwtService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,8 +21,7 @@
import java.io.UnsupportedEncodingException;
import java.util.Random;

import static com.pawwithu.connectdog.error.ErrorCode.ALREADY_EXIST_EMAIL;
import static com.pawwithu.connectdog.error.ErrorCode.EMAIL_SEND_ERROR;
import static com.pawwithu.connectdog.error.ErrorCode.*;

@Service
@RequiredArgsConstructor
Expand All @@ -28,6 +32,7 @@ public class EmailService {
private String authNum; //랜덤 인증 코드
private final VolunteerRepository volunteerRepository;
private final IntermediaryRepository intermediaryRepository;
private final JwtService jwtService;

/**
* 랜덤 인증 코드 생성
Expand Down Expand Up @@ -104,12 +109,41 @@ private String setContext(String code) {
return templateEngine.process("mail", context);
}

public EmailResponse sendEmailWithoutAuth(EmailRequest request) throws BadRequestException {
public VolunteerEmailWithAuthResponse sendEmailToVolunteerWithoutAuth(EmailRequest request) throws BadRequestException {
try{
Long id = volunteerRepository.findByEmail(request.email())
.map(Volunteer::getId)
.orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));

String roleName = "VOLUNTEER";
String accessToken = jwtService.createAccessToken(id, roleName);

// 메일전송에 필요한 정보 설정
MimeMessage emailForm = createEmailForm(request.email());
emailSender.send(emailForm);
return new EmailResponse(authNum);

VolunteerEmailWithAuthResponse response = VolunteerEmailWithAuthResponse.of(authNum, accessToken);
return response;
} catch (UnsupportedEncodingException | MessagingException e){
throw new BadRequestException(EMAIL_SEND_ERROR);
}
}

public IntermediaryEmailWithAuthResponse sendEmailToIntermediaryWithoutAuth(EmailRequest request) {
try{
Long id = intermediaryRepository.findByEmail(request.email())
.map(Intermediary::getId)
.orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));

String roleName = "INTERMEDIARY";
String accessToken = jwtService.createAccessToken(id, roleName);

// 메일전송에 필요한 정보 설정
MimeMessage emailForm = createEmailForm(request.email());
emailSender.send(emailForm);

IntermediaryEmailWithAuthResponse response = IntermediaryEmailWithAuthResponse.of(authNum, accessToken);
return response;
}catch (UnsupportedEncodingException | MessagingException e){
throw new BadRequestException(EMAIL_SEND_ERROR);
}
Expand Down

0 comments on commit c092b6b

Please sign in to comment.