From 53d27cfa1609883d5eefd99ec744885645dcdd8c Mon Sep 17 00:00:00 2001 From: You Jung <80906691+JangYouJung@users.noreply.github.com> Date: Mon, 22 Jul 2024 00:27:15 +0900 Subject: [PATCH] =?UTF-8?q?feature:=20OAuth=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 10 +-- .../server/auth/service/AuthService.java | 2 + .../server/auth/service/OAuthService.java | 88 +++++++++++-------- .../service/dto/OAuthSignInResponseDto.java | 6 +- .../server/user/service/UserService.java | 2 +- .../meltingpot/server/util/ResponseCode.java | 2 + 6 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/main/java/meltingpot/server/auth/controller/AuthController.java b/src/main/java/meltingpot/server/auth/controller/AuthController.java index 14d0286..5f216b2 100644 --- a/src/main/java/meltingpot/server/auth/controller/AuthController.java +++ b/src/main/java/meltingpot/server/auth/controller/AuthController.java @@ -66,16 +66,16 @@ public ResponseEntity> signup( @ApiResponse(responseCode = "CREATED", description = "회원가입 성공"), @ApiResponse(responseCode = "BAD_REQUEST", description = "회원가입 실패") }) - public ResponseEntity oauthSignup( + public ResponseEntity> oauthSignup( @RequestBody @Valid OAuthSignupRequestDto request ){ try{ - return ResponseData.toResponseEntity(oAuthService.oauthSignup(request)); + return ResponseData.toResponseEntity(ResponseCode.OAUTH_SIGNUP_SUCCESS, oAuthService.oauthSignup(request)); }catch ( AuthException e ){ - return ResponseData.toResponseEntity( e.getResponseCode()); + return ResponseData.toResponseEntity( e.getResponseCode(), null); }catch ( IllegalArgumentException e ){ - return ResponseData.toResponseEntity( e.getResponseCode()); + return ResponseData.toResponseEntity( e.getResponseCode(), null); } } @@ -119,7 +119,7 @@ public ResponseEntity> SNSLogin( ){ try{ OAuthSignInResponseDto data = oAuthService.SNSLogin(request); - return ResponseData.toResponseEntity(ResponseCode.SIGNIN_SUCCESS, data); + return ResponseData.toResponseEntity(ResponseCode.OAUTH_SIGNIN_SUCCESS, data); }catch( ResourceNotFoundException e ){ return ResponseData.toResponseEntity(ResponseCode.ACCOUNT_NOT_FOUND, null); diff --git a/src/main/java/meltingpot/server/auth/service/AuthService.java b/src/main/java/meltingpot/server/auth/service/AuthService.java index 0c4e667..94f56af 100644 --- a/src/main/java/meltingpot/server/auth/service/AuthService.java +++ b/src/main/java/meltingpot/server/auth/service/AuthService.java @@ -5,6 +5,7 @@ import meltingpot.server.auth.controller.dto.*; import meltingpot.server.domain.entity.*; import meltingpot.server.domain.entity.enums.Gender; +import meltingpot.server.domain.entity.enums.OAuthType; import meltingpot.server.domain.repository.AccountPushTokenRepository; import meltingpot.server.domain.repository.MailVerificationRepository; import meltingpot.server.exception.*; @@ -105,6 +106,7 @@ public AccountResponseDto signup(SignupRequestDto signupRequest) { .birth(signupRequest.birth()) .nationality(signupRequest.nationality()) .isQuit(false) + .oAuthType(OAuthType.NONE) .build(); account.setProfileImages(signupRequest.profileImages().stream().map( diff --git a/src/main/java/meltingpot/server/auth/service/OAuthService.java b/src/main/java/meltingpot/server/auth/service/OAuthService.java index 082d086..59a39a4 100644 --- a/src/main/java/meltingpot/server/auth/service/OAuthService.java +++ b/src/main/java/meltingpot/server/auth/service/OAuthService.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import meltingpot.server.auth.controller.dto.AccountResponseDto; import meltingpot.server.auth.controller.dto.OAuthSignInRequestDto; import meltingpot.server.auth.controller.dto.OAuthSignupRequestDto; import meltingpot.server.auth.controller.dto.ProfileImageRequestDto; @@ -50,7 +51,9 @@ public class OAuthService { // SNS 회원 가입 - public ResponseCode oauthSignup(OAuthSignupRequestDto signupRequest) { + @Transactional + public OAuthSignInResponseDto oauthSignup(OAuthSignupRequestDto signupRequest) { + // 프로필 사진 개수 확인 if(signupRequest.profileImages().isEmpty()){ throw new AuthException(ResponseCode.PROFILE_IMAGE_LESS_THAN_ONE); @@ -115,7 +118,13 @@ public ResponseCode oauthSignup(OAuthSignupRequestDto signupRequest) { accountRepository.save(account); - return ResponseCode.SIGNUP_SUCCESS; + + return OAuthSignInResponseDto.builder(). + register_required(false) + .nickName(account.getName()) + .email(account.getUsername()) + .tokenDto(setSecurityContext(account, signupRequest.pushToken())) + .build(); } @@ -136,61 +145,66 @@ public OAuthSignInResponseDto SNSLogin(OAuthSignInRequestDto request) throws Exc // 회원 가입이 필요한 경우 return OAuthSignInResponseDto.builder() .register_required(true) - .accessToken(tokenDto.accessToken()) - .refreshToken(tokenDto.refreshToken()) .nickName(kakaoDto.getNickname()) .email(kakaoDto.getEmail()) + .tokenDto(null) .build(); } else { - // 발급 받은 토큰 Spring Security Context에 저장 - OAuthUserDetails oAuthUserDetails= new OAuthUserDetails(account.get()); - Authentication authentication = new UsernamePasswordAuthenticationToken(oAuthUserDetails, null, oAuthUserDetails.getAuthorities()); - SecurityContextHolder.getContext().setAuthentication(authentication); - - // 인증 정보를 기반으로 JWT 토큰 생성 - TokenDto jwtTokenDto = tokenProvider.generateTokenDto(authentication); - - // RefreshToken 저장 - RefreshToken refreshToken = RefreshToken.builder() - .account(account.get()) - .tokenValue(jwtTokenDto.getRefreshToken()) - .build(); - - refreshTokenRepository.save(refreshToken); - - if (!accountPushTokenRepository.existsAccountPushByAccountAndToken(account.get(), request.push_token())) { - AccountPushToken accountPushToken = AccountPushToken.builder() - .account(account.get()) - .token(request.push_token()) - .build(); - - accountPushTokenRepository.save(accountPushToken); - } - - //인증된 Authentication를 SecurityContext에 저장 - SecurityContextHolder.getContext().setAuthentication(authentication); - - - return OAuthSignInResponseDto.builder() - .register_required(false) - .accessToken(jwtTokenDto.getAccessToken()) - .refreshToken(jwtTokenDto.getRefreshToken()) + return OAuthSignInResponseDto.builder(). + register_required(false) .nickName(kakaoDto.getNickname()) .email(kakaoDto.getEmail()) + .tokenDto(setSecurityContext(account.get(), request.push_token())) .build(); } } +// else if(request.type() == OAuthType.APPLE) { +// +// } +// else if(request.type() == OAuthType.GOOGLE) { +// +// } else { throw new NoSuchElementException(); } } + @Transactional + public TokenDto setSecurityContext(Account account, String pushToken ){ + + OAuthUserDetails oAuthUserDetails= new OAuthUserDetails(account); + Authentication authentication = new UsernamePasswordAuthenticationToken(oAuthUserDetails, null, oAuthUserDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + + // 인증 정보를 기반으로 JWT 토큰 생성 + TokenDto jwtTokenDto = tokenProvider.generateTokenDto(authentication); + // RefreshToken 저장 + RefreshToken refreshToken = RefreshToken.builder() + .account(account) + .tokenValue(jwtTokenDto.getRefreshToken()) + .build(); + refreshTokenRepository.save(refreshToken); + // PushToken 저장 + if (!accountPushTokenRepository.existsAccountPushByAccountAndToken(account, pushToken)) { + AccountPushToken accountPushToken = AccountPushToken.builder() + .account(account) + .token(pushToken) + .build(); + accountPushTokenRepository.save(accountPushToken); + } + + //인증된 Authentication를 SecurityContext에 저장 + SecurityContextHolder.getContext().setAuthentication(authentication); + + return jwtTokenDto; + + } } diff --git a/src/main/java/meltingpot/server/auth/service/dto/OAuthSignInResponseDto.java b/src/main/java/meltingpot/server/auth/service/dto/OAuthSignInResponseDto.java index f95500e..f11eb80 100644 --- a/src/main/java/meltingpot/server/auth/service/dto/OAuthSignInResponseDto.java +++ b/src/main/java/meltingpot/server/auth/service/dto/OAuthSignInResponseDto.java @@ -1,13 +1,13 @@ package meltingpot.server.auth.service.dto; import lombok.Builder; +import meltingpot.server.util.TokenDto; @Builder public record OAuthSignInResponseDto( - String accessToken, - String refreshToken, + boolean register_required, String email, String nickName, - boolean register_required + TokenDto tokenDto ) { } diff --git a/src/main/java/meltingpot/server/user/service/UserService.java b/src/main/java/meltingpot/server/user/service/UserService.java index 04fb240..61ffc6f 100644 --- a/src/main/java/meltingpot/server/user/service/UserService.java +++ b/src/main/java/meltingpot/server/user/service/UserService.java @@ -90,7 +90,7 @@ public UserResponseDto updateProfileBio(Account account, UpdateBioRequestDto ser @Transactional public List readProfileImages( long accountId ) { - Account account = accountRepository.findByIdAndDeletedAtIsNull(accountId); + Account account = accountRepository.findByIdAndIsQuitIsFalse(accountId); if(account == null) throw new NoSuchElementException(); List accountProfileImages = accountProfileImageRepository.findAllByAccountAndDeletedAtIsNull(account); diff --git a/src/main/java/meltingpot/server/util/ResponseCode.java b/src/main/java/meltingpot/server/util/ResponseCode.java index 263c921..bfc6cf0 100644 --- a/src/main/java/meltingpot/server/util/ResponseCode.java +++ b/src/main/java/meltingpot/server/util/ResponseCode.java @@ -12,6 +12,7 @@ public enum ResponseCode { /* 200 OK : 요청 성공 */ SIGNIN_SUCCESS(OK, "로그인 성공"), + OAUTH_SIGNIN_SUCCESS(OK, "SNS 로그인 성공"), SIGNOUT_SUCCESS(OK, "로그아웃 성공"), REISSUE_TOKEN_SUCCESS(OK, "토큰 재발급 성공"), MAIL_VERIFICATION_SEND_SUCCESS(OK, "이메일 인증번호 전송 성공"), @@ -60,6 +61,7 @@ public enum ResponseCode { PARTY_REPORT_SUCCESS(CREATED, "파티 신고 성공"), PARTY_CREATE_SUCCESS(CREATED, "파티 생성 성공"), IMAGE_URL_GENERATE_SUCCESS(CREATED, "이미지 URL 생성 성공"), + OAUTH_SIGNUP_SUCCESS(CREATED, "SNS 회원가입 성공"), /* 400 BAD_REQUEST : 잘못된 요청 */