Skip to content

Commit

Permalink
feature: OAuth 회원가입 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
JangYouJung committed Jul 21, 2024
1 parent d6b7b9c commit 53d27cf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public ResponseEntity<ResponseData<AccountResponseDto>> signup(
@ApiResponse(responseCode = "CREATED", description = "회원가입 성공"),
@ApiResponse(responseCode = "BAD_REQUEST", description = "회원가입 실패")
})
public ResponseEntity<ResponseData> oauthSignup(
public ResponseEntity<ResponseData<OAuthSignInResponseDto>> 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);
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public ResponseEntity<ResponseData<OAuthSignInResponseDto>> 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);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/meltingpot/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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(
Expand Down
88 changes: 51 additions & 37 deletions src/main/java/meltingpot/server/auth/service/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

}

Expand All @@ -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;

}

}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public UserResponseDto updateProfileBio(Account account, UpdateBioRequestDto ser

@Transactional
public List<UserImagesResponseDto> readProfileImages( long accountId ) {
Account account = accountRepository.findByIdAndDeletedAtIsNull(accountId);
Account account = accountRepository.findByIdAndIsQuitIsFalse(accountId);
if(account == null) throw new NoSuchElementException();

List<AccountProfileImage> accountProfileImages = accountProfileImageRepository.findAllByAccountAndDeletedAtIsNull(account);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/meltingpot/server/util/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "이메일 인증번호 전송 성공"),
Expand Down Expand Up @@ -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 : 잘못된 요청 */
Expand Down

0 comments on commit 53d27cf

Please sign in to comment.