Skip to content

Commit

Permalink
Merge pull request #195 from TEAM-MODDY/feat/#194
Browse files Browse the repository at this point in the history
#194 [feat] Redis 적용 및 번호 인증 로직 수정
  • Loading branch information
KWY0218 authored Jan 26, 2024
2 parents cb27e8b + 3e5b8ed commit 79934b4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 74 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.9.0'

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/moddy/server/common/util/SmsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@

import com.moddy.server.external.sms.SmsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
public class SmsUtil {
private static final String VERIFICATION_MESSAGE_FORM = "[%s] moddy에서 보낸 인증번호입니다.";
private static final String OFFER_MESSAGE_FORM = "[moddy] %s님, 새로운 헤어모델 제안이 도착했어요!\nmoddy.kr";
private static final int VERIFICATION_EXPIRE_TIME = 3;
private final SmsService smsService;
private final RedisTemplate<String, String> redisTemplate;

public boolean sendVerificationCode(final String to, final String verificationCode) throws IOException {
final String verificationCodeMessage = String.format(VERIFICATION_MESSAGE_FORM, verificationCode);
return smsService.sendSms(to, verificationCodeMessage);
}

public void saveVerificationCode(final String to, final String verificationCode) {
redisTemplate.opsForValue().set(to, verificationCode, VERIFICATION_EXPIRE_TIME, TimeUnit.MINUTES);
}

public boolean isVerificationCode(final String to) {
return redisTemplate.opsForValue().get(to) != null;
}

public String getVerificationCode(final String to) {
return redisTemplate.opsForValue().get(to);
}

public void deleteVerificationCode(final String to) {
redisTemplate.delete(to);
}

public boolean sendOfferToModel(final String to, final String modelName) throws IOException {
final String modelMessage = String.format(OFFER_MESSAGE_FORM, modelName);
return smsService.sendSms(to, modelMessage);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/moddy/server/config/redis/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.moddy.server.config.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

@Bean
public RedisTemplate<String, String> getRedisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
36 changes: 0 additions & 36 deletions src/main/java/com/moddy/server/domain/verify/UserVerification.java

This file was deleted.

This file was deleted.

35 changes: 9 additions & 26 deletions src/main/java/com/moddy/server/service/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@
import com.moddy.server.domain.region.repository.RegionJpaRepository;
import com.moddy.server.domain.user.User;
import com.moddy.server.domain.user.repository.UserRepository;
import com.moddy.server.domain.verify.UserVerification;
import com.moddy.server.domain.verify.repository.UserVerificationRepository;
import com.moddy.server.external.kakao.service.KakaoSocialService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.moddy.server.common.exception.enums.ErrorCode.EXPIRE_VERIFICATION_CODE_EXCEPTION;
import static com.moddy.server.common.exception.enums.ErrorCode.INVALID_PHONE_NUMBER_EXCEPTION;
import static com.moddy.server.common.exception.enums.ErrorCode.NOT_FOUND_VERIFICATION_CODE_EXCEPTION;
import static com.moddy.server.common.exception.enums.ErrorCode.NOT_MATCH_VERIFICATION_CODE_EXCEPTION;
import static com.moddy.server.common.exception.enums.ErrorCode.USER_NOT_FOUND_EXCEPTION;


@Service
@RequiredArgsConstructor
public class AuthService {
Expand All @@ -40,7 +37,6 @@ public class AuthService {
private final KakaoSocialService kakaoSocialService;
private final UserRepository userRepository;
private final RegionJpaRepository regionJpaRepository;
private final UserVerificationRepository userVerificationRepository;

public LoginResponseDto login(final String baseUrl, final String kakaoCode) {
String kakaoId = kakaoSocialService.getIdFromKakao(baseUrl, kakaoCode);
Expand Down Expand Up @@ -83,37 +79,24 @@ public UserCreateResponse createUserToken(String useId) {
}

@Transactional
public void sendVerificationCodeMessageToUser(String phoneNumber) throws IOException {
Optional<UserVerification> userVerification = userVerificationRepository.findByPhoneNumber(phoneNumber);
if (userVerification.isPresent()) {
userVerificationRepository.deleteByPhoneNumber(phoneNumber);
}
public void sendVerificationCodeMessageToUser(final String phoneNumber) throws IOException {
final String verificationCode = VerificationCodeGenerator.generate();

String verificationCode = VerificationCodeGenerator.generate();
if (!smsUtil.sendVerificationCode(phoneNumber, verificationCode))
throw new BadRequestException(INVALID_PHONE_NUMBER_EXCEPTION);

UserVerification newUserVerification = UserVerification.builder()
.phoneNumber(phoneNumber)
.verificationCode(verificationCode)
.build();
userVerificationRepository.save(newUserVerification);
smsUtil.saveVerificationCode(phoneNumber, verificationCode);
}

@Transactional
public void verifyCode(String phoneNumber, String verificationCode) {
UserVerification userVerification = userVerificationRepository.findByPhoneNumber(phoneNumber)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_VERIFICATION_CODE_EXCEPTION));

if (userVerification.isExpireCode(LocalDateTime.now())) {
userVerificationRepository.deleteByPhoneNumber(phoneNumber);
throw new BadRequestException(EXPIRE_VERIFICATION_CODE_EXCEPTION);
}
public void verifyCode(final String phoneNumber, final String verificationCode) {
if (!smsUtil.isVerificationCode(phoneNumber))
throw new NotFoundException(NOT_FOUND_VERIFICATION_CODE_EXCEPTION);

if (!verificationCode.equals(userVerification.getVerificationCode()))
if (!smsUtil.getVerificationCode(phoneNumber).equals(verificationCode))
throw new BadRequestException(NOT_MATCH_VERIFICATION_CODE_EXCEPTION);

userVerificationRepository.deleteByPhoneNumber(phoneNumber);
smsUtil.deleteVerificationCode(phoneNumber);
}

public void logout(Long userId) {
Expand Down

0 comments on commit 79934b4

Please sign in to comment.