Skip to content

Commit

Permalink
Merge pull request #19 from KusitmsHDmedi/feature/14-user
Browse files Browse the repository at this point in the history
[feat] 초대 code 생성 api 구현
  • Loading branch information
RyuKwanKon authored Sep 13, 2023
2 parents a00e55a + 995c585 commit 11e13a1
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import com.kusithm.hdmedi_server.domain.user.dto.request.UserAuthRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserSignUpRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.AuthCodeResponseDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.UserAuthResponseDto;
import com.kusithm.hdmedi_server.domain.user.service.AuthService;
import com.kusithm.hdmedi_server.global.common.BaseResponse;
import com.kusithm.hdmedi_server.global.common.SuccessCode;
import com.kusithm.hdmedi_server.global.config.auth.AuthenticatedUserId;
import lombok.RequiredArgsConstructor;
import org.hibernate.query.criteria.JpaCriteriaUpdate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RequestMapping("/api/user")
Expand All @@ -23,17 +23,24 @@ public class AuthController {

@PostMapping("/signIn")
public ResponseEntity<BaseResponse<?>> signIn(@RequestHeader("Authorization") final String token,
@RequestBody final UserAuthRequestDto userAuthRequestDto){
@RequestBody final UserAuthRequestDto userAuthRequestDto) {
final UserAuthResponseDto responseDto = authService.signIn(token, userAuthRequestDto);
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, responseDto));
}

@PostMapping("/signUp")
public ResponseEntity<BaseResponse<?>> signUp(@RequestHeader("Authorization") final String token,
@RequestBody final UserSignUpRequestDto userSignUpRequestDto){
@RequestBody final UserSignUpRequestDto userSignUpRequestDto) {
final UserAuthResponseDto responseDto = authService.signUp(token, userSignUpRequestDto);
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, responseDto));
return ResponseEntity.status(HttpStatus.CREATED)
.body(BaseResponse.of(SuccessCode.CREATED, responseDto));
}

@GetMapping("/authCode")
public ResponseEntity<BaseResponse<?>> createAuthCode(@AuthenticatedUserId final Long userId) {
final AuthCodeResponseDto responseDto = authService.createAuthCode(userId);
return ResponseEntity.status(HttpStatus.CREATED)
.body(BaseResponse.of(SuccessCode.CREATED, responseDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.kusithm.hdmedi_server.domain.user.domain;

import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.redis.core.RedisHash;

@AllArgsConstructor
@Builder
@Getter
@RedisHash(value = "authCode", timeToLive = 604800000)
public class AuthCode {
@Id
private String authCode;
private Long id;

public static AuthCode createAuthCode(String authCode, Long id){
return AuthCode.builder()
.authCode(authCode)
.id(id)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kusithm.hdmedi_server.domain.user.dto.response;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class AuthCodeResponseDto {
private String authCode;

public static AuthCodeResponseDto of(String authCode){
return AuthCodeResponseDto.builder()
.authCode(authCode)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kusithm.hdmedi_server.domain.user.repository;

import com.kusithm.hdmedi_server.domain.user.domain.AuthCode;
import org.springframework.data.repository.CrudRepository;

public interface AuthCodeRepository extends CrudRepository<AuthCode, Long> {
boolean existsByAuthCode(String authCode);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.kusithm.hdmedi_server.domain.user.service;

import com.kusithm.hdmedi_server.domain.user.auth.naver.NaverOAuthProvider;
import com.kusithm.hdmedi_server.domain.user.domain.AuthCode;
import com.kusithm.hdmedi_server.domain.user.domain.Platform;
import com.kusithm.hdmedi_server.domain.user.domain.User;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserAuthRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserSignUpRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.AuthCodeResponseDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.UserAuthResponseDto;
import com.kusithm.hdmedi_server.domain.user.repository.AuthCodeRepository;
import com.kusithm.hdmedi_server.domain.user.repository.RefreshTokenRepository;
import com.kusithm.hdmedi_server.domain.user.repository.UserRepository;
import com.kusithm.hdmedi_server.global.config.jwt.JwtProvider;
Expand All @@ -16,6 +19,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;

import static com.kusithm.hdmedi_server.domain.user.domain.RefreshToken.createRefreshToken;
import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.DUPLICATE_USER;
import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.USER_NOT_FOUND;
Expand All @@ -26,9 +31,13 @@
public class AuthService {
private final NaverOAuthProvider naverOAuthProvider;
private final RefreshTokenRepository refreshTokenRepository;
private final AuthCodeRepository authCodeRepository;
private final UserRepository userRepository;
private final JwtProvider jwtProvider;

private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final int CODE_LENGTH = 6;

public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto) {
Platform platform = Platform.getEnumPlatformFrom(requestDto.getPlatform());
String platformId = getPlatformId(token);
Expand All @@ -38,7 +47,7 @@ public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto) {
return UserAuthResponseDto.of(issuedToken, findUser);
}

public UserAuthResponseDto signUp(String token, UserSignUpRequestDto requestDto){
public UserAuthResponseDto signUp(String token, UserSignUpRequestDto requestDto) {
Platform platform = Platform.getEnumPlatformFrom(requestDto.getPlatform());
String platformId = getPlatformId(token);
validateDuplicateUser(platform, platformId);
Expand All @@ -48,6 +57,39 @@ public UserAuthResponseDto signUp(String token, UserSignUpRequestDto requestDto)
return UserAuthResponseDto.of(issuedToken, saveUser);
}

public AuthCodeResponseDto createAuthCode(Long userId) {
String authCode = createAuthCodeAtSecureRandom(new SecureRandom());
AuthCode createdAuthCode = AuthCode.createAuthCode(authCode, userId);
saveAuthCode(createdAuthCode);
return AuthCodeResponseDto.of(createdAuthCode.getAuthCode());
}

private void saveAuthCode(AuthCode createdAuthCode) {
authCodeRepository.save(createdAuthCode);
}

private boolean duplicateAuthCode(String authCode) {
return authCodeRepository.existsByAuthCode(authCode);
}

private String createAuthCodeAtSecureRandom(SecureRandom random) {
StringBuilder codeBuilder;
do {
codeBuilder = createAuthCodeWithStringBuilder(random);
} while (duplicateAuthCode(codeBuilder.toString()));
return codeBuilder.toString();
}

private StringBuilder createAuthCodeWithStringBuilder(SecureRandom random){
StringBuilder codeBuilder = new StringBuilder();
for (int i = 0; i < CODE_LENGTH; i++) {
int randomIndex = random.nextInt(CHARACTERS.length());
char randomChar = CHARACTERS.charAt(randomIndex);
codeBuilder.append(randomChar);
}
return codeBuilder;
}

private User getUser(Platform platform, String platformId) {
return userRepository.findUserByPlatformAndPlatformId(platform, platformId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
Expand Down

0 comments on commit 11e13a1

Please sign in to comment.