-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 특정 이메일 주소로 인증코드를 보내는 기능을 만들었어요. - 이메일로 발송된 인증코드를 입력받으면 인증 완료하는 기능을 만들었어요.
- Loading branch information
Showing
17 changed files
with
308 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/bamdoliro/sinabro/application/user/SendVerificationUseCase.java
This file contains 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,34 @@ | ||
package com.bamdoliro.sinabro.application.user; | ||
|
||
import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification; | ||
import com.bamdoliro.sinabro.infrastructure.mail.MailService; | ||
import com.bamdoliro.sinabro.infrastructure.persistence.user.SignUpVerificationRepository; | ||
import com.bamdoliro.sinabro.presentation.user.dto.request.SendVerificationRequest; | ||
import com.bamdoliro.sinabro.shared.annotation.UseCase; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@UseCase | ||
public class SendVerificationUseCase { | ||
|
||
private final MailService mailService; | ||
private final SignUpVerificationRepository signUpVerificationRepository; | ||
|
||
public void execute(SendVerificationRequest request) { | ||
SignUpVerification signUpVerification = new SignUpVerification(request.getEmail()); | ||
|
||
String subject = "시나브로 회원가입 인증번호"; | ||
String text = String.format( | ||
"[시나브로] 회원가입 인증번호는 [%s]입니다.", | ||
signUpVerification.getCode() | ||
); | ||
|
||
mailService.execute( | ||
subject, | ||
request.getEmail(), | ||
text | ||
); | ||
|
||
signUpVerificationRepository.save(signUpVerification); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/bamdoliro/sinabro/application/user/VerifyUseCase.java
This file contains 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,34 @@ | ||
package com.bamdoliro.sinabro.application.user; | ||
|
||
import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification; | ||
import com.bamdoliro.sinabro.domain.user.exception.VerificationCodeMismatchException; | ||
import com.bamdoliro.sinabro.domain.user.service.VerificationFacade; | ||
import com.bamdoliro.sinabro.infrastructure.persistence.user.SignUpVerificationRepository; | ||
import com.bamdoliro.sinabro.presentation.user.dto.request.VerifyRequest; | ||
import com.bamdoliro.sinabro.shared.annotation.UseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@UseCase | ||
public class VerifyUseCase { | ||
|
||
private final SignUpVerificationRepository signUpVerificationRepository; | ||
private final VerificationFacade verificationFacade; | ||
|
||
@Transactional | ||
public void execute(VerifyRequest request) { | ||
SignUpVerification signUpVerification = verificationFacade.getVerification(request.getEmail()); | ||
System.out.println(request.getCode()); | ||
System.out.println(signUpVerification.getCode()); | ||
|
||
if (!signUpVerification.getCode().equals(request.getCode())) { | ||
throw new VerificationCodeMismatchException(); | ||
} | ||
|
||
signUpVerificationRepository.updateSignUpVerification( | ||
signUpVerification.getEmail(), | ||
true | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/bamdoliro/sinabro/domain/user/domain/SignUpVerification.java
This file contains 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,31 @@ | ||
package com.bamdoliro.sinabro.domain.user.domain; | ||
|
||
import com.bamdoliro.sinabro.shared.util.RandomCodeUtil; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@RedisHash(value = "signup-verification", timeToLive = 60 * 5) | ||
public class SignUpVerification { | ||
|
||
@Id | ||
private String email; | ||
|
||
private String code; | ||
|
||
private Boolean isVerified; | ||
|
||
public SignUpVerification(String email) { | ||
this.email = email; | ||
this.code = RandomCodeUtil.generate(6); | ||
this.isVerified = false; | ||
} | ||
|
||
public void verify() { | ||
this.isVerified = true; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n/java/com/bamdoliro/sinabro/domain/user/exception/VerificationCodeMismatchException.java
This file contains 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,10 @@ | ||
package com.bamdoliro.sinabro.domain.user.exception; | ||
|
||
import com.bamdoliro.sinabro.domain.user.exception.error.UserErrorProperty; | ||
import com.bamdoliro.sinabro.shared.error.SinabroException; | ||
|
||
public class VerificationCodeMismatchException extends SinabroException { | ||
public VerificationCodeMismatchException() { | ||
super(UserErrorProperty.VERIFICATION_CODE_MISMATCH); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/bamdoliro/sinabro/domain/user/exception/VerifyingHasFailedException.java
This file contains 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,10 @@ | ||
package com.bamdoliro.sinabro.domain.user.exception; | ||
|
||
import com.bamdoliro.sinabro.domain.user.exception.error.UserErrorProperty; | ||
import com.bamdoliro.sinabro.shared.error.SinabroException; | ||
|
||
public class VerifyingHasFailedException extends SinabroException { | ||
public VerifyingHasFailedException() { | ||
super(UserErrorProperty.VERIFYING_HAS_FAILED); | ||
} | ||
} |
This file contains 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
21 changes: 21 additions & 0 deletions
21
src/main/java/com/bamdoliro/sinabro/domain/user/service/VerificationFacade.java
This file contains 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,21 @@ | ||
package com.bamdoliro.sinabro.domain.user.service; | ||
|
||
import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification; | ||
import com.bamdoliro.sinabro.domain.user.exception.VerifyingHasFailedException; | ||
import com.bamdoliro.sinabro.infrastructure.persistence.user.SignUpVerificationRepository; | ||
import com.bamdoliro.sinabro.shared.annotation.UseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@UseCase | ||
public class VerificationFacade { | ||
|
||
private final SignUpVerificationRepository signUpVerificationRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public SignUpVerification getVerification(String id) { | ||
return signUpVerificationRepository.findById(id) | ||
.orElseThrow(VerifyingHasFailedException::new); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/bamdoliro/sinabro/infrastructure/mail/MailService.java
This file contains 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,28 @@ | ||
package com.bamdoliro.sinabro.infrastructure.mail; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class MailService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
|
||
@Value("${spring.mail.username}") | ||
private String from; | ||
|
||
public void execute(String subject, String to, String text) { | ||
|
||
SimpleMailMessage message = new SimpleMailMessage(); | ||
message.setFrom(from); | ||
message.setTo(to); | ||
message.setSubject(subject); | ||
message.setText(text); | ||
|
||
javaMailSender.send(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...a/com/bamdoliro/sinabro/infrastructure/persistence/user/SignUpVerificationRepository.java
This file contains 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,7 @@ | ||
package com.bamdoliro.sinabro.infrastructure.persistence.user; | ||
|
||
import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface SignUpVerificationRepository extends CrudRepository<SignUpVerification, String>, VerificationRedisRepository { | ||
} |
6 changes: 6 additions & 0 deletions
6
...va/com/bamdoliro/sinabro/infrastructure/persistence/user/VerificationRedisRepository.java
This file contains 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,6 @@ | ||
package com.bamdoliro.sinabro.infrastructure.persistence.user; | ||
|
||
public interface VerificationRedisRepository { | ||
|
||
void updateSignUpVerification(String email, boolean verified); | ||
} |
23 changes: 23 additions & 0 deletions
23
...om/bamdoliro/sinabro/infrastructure/persistence/user/VerificationRedisRepositoryImpl.java
This file contains 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,23 @@ | ||
package com.bamdoliro.sinabro.infrastructure.persistence.user; | ||
|
||
import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.PartialUpdate; | ||
import org.springframework.data.redis.core.RedisKeyValueTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class VerificationRedisRepositoryImpl implements VerificationRedisRepository { | ||
|
||
private final RedisKeyValueTemplate template; | ||
|
||
@Override | ||
public void updateSignUpVerification(String email, boolean verified) { | ||
PartialUpdate<SignUpVerification> update = new PartialUpdate<>(email, SignUpVerification.class) | ||
.set("isVerified", verified) | ||
.refreshTtl(true); | ||
|
||
template.update(update); | ||
} | ||
} |
40 changes: 37 additions & 3 deletions
40
src/main/java/com/bamdoliro/sinabro/presentation/user/UserController.java
This file contains 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
15 changes: 15 additions & 0 deletions
15
...ain/java/com/bamdoliro/sinabro/presentation/user/dto/request/SendVerificationRequest.java
This file contains 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,15 @@ | ||
package com.bamdoliro.sinabro.presentation.user.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class SendVerificationRequest { | ||
|
||
@NotBlank(message = "필수값입니다.") | ||
private String email; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/bamdoliro/sinabro/presentation/user/dto/request/VerifyRequest.java
This file contains 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,22 @@ | ||
package com.bamdoliro.sinabro.presentation.user.dto.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class VerifyRequest { | ||
|
||
@NotBlank(message = "필수값입니다.") | ||
@Email(message = "이메일 형식이어야 합니다") | ||
private String email; | ||
|
||
@NotBlank(message = "필수값입니다.") | ||
@Size(min = 6, max = 6, message = "6자여야 합니다.") | ||
private String code; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/bamdoliro/sinabro/shared/util/RandomCodeUtil.java
This file contains 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,10 @@ | ||
package com.bamdoliro.sinabro.shared.util; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
|
||
public class RandomCodeUtil { | ||
|
||
public static String generate(int count) { | ||
return RandomStringUtils.randomNumeric(count); | ||
} | ||
} |
This file contains 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