Skip to content

Commit

Permalink
refactor: do signin on one class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 12, 2024
1 parent e68227a commit 7c0ab70
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import co.orange.ddanzi.common.error.Error;
import co.orange.ddanzi.common.response.Success;
import co.orange.ddanzi.domain.user.enums.LoginType;
import co.orange.ddanzi.dto.auth.RefreshTokenRequestDto;
import co.orange.ddanzi.dto.auth.SigninRequestDto;
import co.orange.ddanzi.dto.auth.VerifyRequestDto;
Expand All @@ -22,7 +21,6 @@
@RequestMapping("/api/v1/auth")
public class AuthController {
private final AuthService authService;
private final OAuthService oAuthService;
private final JwtUtils jwtUtils;

@PostMapping("/signin/test")
Expand All @@ -32,9 +30,7 @@ ApiResponse<?> test(@RequestBody SigninRequestDto requestDto){

@PostMapping("/signin")
ApiResponse<?> signin(@RequestBody SigninRequestDto requestDto) throws JsonProcessingException {
if(requestDto.getType().equals(LoginType.KAKAO))
return oAuthService.kakaoSignIn(requestDto);
return oAuthService.kakaoSignIn(requestDto);
return authService.signin(requestDto);
}

@PostMapping("/refreshtoken")
Expand Down
56 changes: 47 additions & 9 deletions src/main/java/co/orange/ddanzi/service/auth/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package co.orange.ddanzi.service.auth;

import co.orange.ddanzi.domain.user.Authentication;
import co.orange.ddanzi.domain.user.Device;
import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.domain.user.enums.FcmCase;
import co.orange.ddanzi.domain.user.enums.LoginType;
import co.orange.ddanzi.domain.user.enums.UserStatus;
import co.orange.ddanzi.dto.auth.RefreshTokenResponseDto;
import co.orange.ddanzi.dto.auth.SigninResponseDto;
import co.orange.ddanzi.dto.auth.VerifyRequestDto;
import co.orange.ddanzi.dto.auth.VerifyResponseDto;
import co.orange.ddanzi.dto.auth.*;
import co.orange.ddanzi.common.error.Error;
import co.orange.ddanzi.common.response.ApiResponse;
import co.orange.ddanzi.common.response.Success;
import co.orange.ddanzi.global.jwt.AuthUtils;
import co.orange.ddanzi.global.jwt.JwtUtils;
import co.orange.ddanzi.repository.AuthenticationRepository;
import co.orange.ddanzi.repository.DeviceRepository;
import co.orange.ddanzi.repository.UserRepository;
import co.orange.ddanzi.service.ItemService;
import co.orange.ddanzi.service.OrderService;
import co.orange.ddanzi.service.PaymentService;
import co.orange.ddanzi.service.TermService;
import co.orange.ddanzi.service.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
Expand All @@ -34,13 +37,39 @@
public class AuthService {
private final JwtUtils jwtUtils;
private final AuthUtils authUtils;

private final OAuthService oAuthService;
private final UserRepository userRepository;
private final AuthenticationRepository authenticationRepository;
private final DeviceRepository deviceRepository;

private final FcmService fcmService;

@Autowired
TermService termService;

@Transactional
public ApiResponse<?> signin(SigninRequestDto requestDto) throws JsonProcessingException {
User user;
if(requestDto.getType().equals(LoginType.KAKAO)) {
user = oAuthService.kakaoSignIn(requestDto);
}
else{
user = oAuthService.appleSignin(requestDto);
}
connectUserAndDevice(user, requestDto);
fcmService.registerFcmToken(user,requestDto.getFcmToken());

if(user.getStatus() == UserStatus.DELETE||user.getStatus()== UserStatus.SLEEP)
user.updateStatus(UserStatus.ACTIVATE);

SigninResponseDto responseDto = SigninResponseDto.builder()
.accesstoken(jwtUtils.createAccessToken(user.getEmail()))
.refreshtoken(jwtUtils.createRefreshToken(user.getEmail()))
.nickname(user.getNickname())
.status(user.getStatus())
.build();
return ApiResponse.onSuccess(Success.SIGNIN_KAKAO_SUCCESS, responseDto);
}

@Transactional
public ApiResponse<?> testSignin(String idToken){
Expand Down Expand Up @@ -113,6 +142,15 @@ public ApiResponse<?> withdraw(){
return ApiResponse.onSuccess(Success.DELETE_USER_SUCCESS, Map.of("nickname", user.getNickname()));
}

public void connectUserAndDevice(User user, SigninRequestDto requestDto) {
log.info("유저의 디바이스 토큰 저장");
Device device = Device.builder()
.user(user)
.deviceToken(requestDto.getDevicetoken())
.type(requestDto.getDeviceType())
.build();
deviceRepository.save(device);
}
}


0 comments on commit 7c0ab70

Please sign in to comment.