Skip to content

Commit

Permalink
fix: 회원가입/로그인 단일 API로 구현 #8
Browse files Browse the repository at this point in the history
클라이언트 개발자의 요청으로 단일 API로 구현
하지만 고정은 아니고 추후에 회원가입 시, 온보딩을 수행하고 데이터를 입력받는 등의 플로우 변경이 발생 시에 SingUpRequest DTO와 LoginRequest DTO를 분리하기 위해 API또한 분리할 예정
  • Loading branch information
PgmJun committed Jan 23, 2024
1 parent 97016e7 commit 2cee355
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.nice.petudio.api.controller.auth.dto.request.LoginRequest;
import com.nice.petudio.api.controller.auth.dto.request.ReissueRequest;
import com.nice.petudio.api.controller.auth.dto.request.SignUpRequest;
import com.nice.petudio.api.controller.auth.vo.TokenVO;
import com.nice.petudio.api.controller.auth.service.AuthService;
import com.nice.petudio.api.controller.auth.service.AuthServiceProvider;
import com.nice.petudio.api.controller.auth.service.CommonAuthService;
import com.nice.petudio.api.controller.auth.service.CreateTokenService;
import com.nice.petudio.api.controller.auth.vo.TokenVO;
import com.nice.petudio.api.dto.ApiResponse;
import com.nice.petudio.common.auth.auth.Auth;
import com.nice.petudio.common.auth.resolver.MemberId;
Expand All @@ -17,7 +16,6 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -35,17 +33,6 @@ public class AuthController {
private static final String ACCESS_TOKEN_COOKIE_NAME = "accessToken";
private static final String REFRESH_TOKEN_COOKIE_NAME = "refreshToken";

@Operation(summary = "OAuth2 소셜 회원가입")
@ResponseStatus(HttpStatus.OK)
@PostMapping("/auth/signup")
public ApiResponse<?> signUp(@Valid @RequestBody final SignUpRequest request, HttpServletResponse response) {
AuthService authService = authServiceProvider.getAuthService(request.getSocialType());
Long memberId = authService.signUp(request);

addTokensToCookie(createTokenService.createTokenInfo(memberId), response);

return ApiResponse.success();
}

@Operation(summary = "OAuth2 소셜 로그인")
@ResponseStatus(HttpStatus.OK)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@


import com.nice.petudio.api.controller.auth.dto.request.LoginRequest;
import com.nice.petudio.api.controller.auth.dto.request.SignUpRequest;
import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.SocialType;
import java.util.Optional;

public interface AuthService {
String SIGN_UP_LOG_MESSAGE = "회원가입 성공 - [memberId(%d) / SocialType(%s)]";

Long signUp(SignUpRequest request);
Long signUp(LoginRequest request);

Long login(LoginRequest request);

void writeSignUpLog(Long memberId, SocialType socialType);

default Long loginOrSignUp(LoginRequest request, Optional<Member> member) {
if(member.isEmpty()) {
return signUp(request);
}

Member loggedInMember = member.get();
loggedInMember.updateFcmToken(request.getFcmToken());
return loggedInMember.getId();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nice.petudio.api.controller.auth.service.impl;

import com.nice.petudio.api.controller.auth.dto.request.LoginRequest;
import com.nice.petudio.api.controller.auth.dto.request.SignUpRequest;
import com.nice.petudio.api.controller.auth.service.AuthService;
import com.nice.petudio.api.controller.member.dto.CreateMemberRequest;
import com.nice.petudio.api.controller.member.service.MemberCommandService;
Expand All @@ -11,37 +10,46 @@
import com.nice.petudio.domain.member.repository.MemberRepository;
import com.nice.petudio.external.client.auth.google.GoogleApiCaller;
import com.nice.petudio.external.client.auth.google.dto.response.GoogleProfileResponse;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Slf4j
@Service
@Transactional
public class GoogleAuthService implements AuthService {

private final GoogleApiCaller googleApiCaller;
private final GoogleApiCaller googleApiCaller;

private final MemberRepository memberRepository;
private final MemberRepository memberRepository;

private final MemberCommandService memberCommandService;
private final MemberCommandService memberCommandService;

@Override
public Long signUp(SignUpRequest request) {
GoogleProfileResponse profileInfo = googleApiCaller.getProfileInfo(request.getToken());
CreateMemberRequest createMemberRequest = CreateMemberRequest.of(profileInfo.getId(), SocialType.GOOGLE,
request.getFcmToken());
@Override
public Long signUp(LoginRequest request) {
GoogleProfileResponse profileInfo = googleApiCaller.getProfileInfo(request.getToken());
CreateMemberRequest createMemberRequest = CreateMemberRequest.of(profileInfo.getId(), SocialType.GOOGLE,
request.getFcmToken());
Long memberId = memberCommandService.registerMember(createMemberRequest);

return memberCommandService.registerMember(createMemberRequest);
}
writeSignUpLog(memberId, SocialType.GOOGLE);
return memberId;
}

@Override
public Long login(LoginRequest request) {
GoogleProfileResponse response = googleApiCaller.getProfileInfo(request.getToken());
Member member = MemberServiceUtils.findMemberBySocialIdAndSocialType(memberRepository, response.getId(),
SocialType.GOOGLE);
member.updateFcmToken(request.getFcmToken());
@Override
public Long login(LoginRequest request) {
GoogleProfileResponse response = googleApiCaller.getProfileInfo(request.getToken());
Optional<Member> member = MemberServiceUtils.findOptionalMemberBySocialInfo(memberRepository, response.getId(),
SocialType.GOOGLE);

return member.getId();
}
return loginOrSignUp(request, member);
}

@Override
public void writeSignUpLog(Long memberId, SocialType socialType) {
log.info(String.format(SIGN_UP_LOG_MESSAGE, memberId, socialType));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nice.petudio.api.controller.auth.service.impl;

import com.nice.petudio.api.controller.auth.dto.request.LoginRequest;
import com.nice.petudio.api.controller.auth.dto.request.SignUpRequest;
import com.nice.petudio.api.controller.auth.service.AuthService;
import com.nice.petudio.api.controller.member.dto.CreateMemberRequest;
import com.nice.petudio.api.controller.member.service.MemberCommandService;
Expand All @@ -11,11 +10,14 @@
import com.nice.petudio.domain.member.repository.MemberRepository;
import com.nice.petudio.external.client.auth.kakao.KakaoApiCaller;
import com.nice.petudio.external.client.auth.kakao.dto.response.KakaoProfileResponse;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Slf4j
@Service
@Transactional
public class KakaoAuthService implements AuthService {
Expand All @@ -27,21 +29,28 @@ public class KakaoAuthService implements AuthService {
private final MemberCommandService memberCommandService;

@Override
public Long signUp(SignUpRequest request) {
public Long signUp(LoginRequest request) {
KakaoProfileResponse profileInfo = kakaoApiCaller.getProfileInfo(request.getToken());
CreateMemberRequest createMemberRequest = CreateMemberRequest.of(profileInfo.getId(), SocialType.KAKAO,
request.getFcmToken());
Long memberId = memberCommandService.registerMember(createMemberRequest);

return memberCommandService.registerMember(createMemberRequest);
writeSignUpLog(memberId, SocialType.KAKAO);
return memberId;
}

@Override
public Long login(LoginRequest request) {
KakaoProfileResponse response = kakaoApiCaller.getProfileInfo(request.getToken());
Member member = MemberServiceUtils.findMemberBySocialIdAndSocialType(memberRepository, response.getId(),
SocialType.KAKAO);
member.updateFcmToken(request.getFcmToken());
Optional<Member> member = MemberServiceUtils.findOptionalMemberBySocialInfo(
memberRepository, response.getId(),
SocialType.KAKAO);

return member.getId();
return loginOrSignUp(request, member);
}

@Override
public void writeSignUpLog(Long memberId, SocialType socialType) {
log.info(String.format(SIGN_UP_LOG_MESSAGE, memberId, socialType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public static void validateNotExistsMember(MemberRepository memberRepository, St
}
}

public static Member findMemberBySocialIdAndSocialType(MemberRepository memberRepository, String socialId,
SocialType socialType) {
public static Member findMemberBySocialInfo(MemberRepository memberRepository, String socialId,
SocialType socialType) {
Optional<Member> member = memberRepository.findMemberBySocialIdAndSocialType(socialId, socialType);
return member.orElseThrow(() -> new NotFoundException(
ErrorCode.NOT_FOUND_MEMBER_INFO_EXCEPTION,
String.format("존재하지 않는 회원 소셜정보 (%s - %s) 입니다", socialType, socialId)));
}

public static Optional<Member> findOptionalMemberBySocialInfo(MemberRepository memberRepository, String socialId,
SocialType socialType) {
return memberRepository.findMemberBySocialIdAndSocialType(socialId, socialType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
@Getter
public enum ErrorCode {
// Validation Exception
VALIDATION_EXCEPTION("V001", "유효하지 않은 요청 값입니다."),
VALIDATION_EXCEPTION("V001", "유효하지 않은 요청입니다."),
BAD_REQUEST_EXCEPTION("V002", "잘못된 요청입니다."),
METHOD_NOT_ALLOWED_EXCEPTION("V003", "지원하지 않는 HTTP 메소드입니다."),
UNSUPPORTED_MEDIA_TYPE("V004", "허용하지 않는 미디어 타입입니다."),
INVALID_JWT_TOKEN_EXCEPTION("V005", "존재하지 않거나 잘못된 JWT 토큰 형식입니다."),
INVALID_OAUTH2_ACCESS_TOKEN_EXCEPTION("V006", "존재하지 않거나 잘못된 OAuth2 Access 토큰 입니다."),
NO_RESOURCE_FOUND_EXCEPTION("V007", "존재하지 않는 API 주소입니다."),

// UnAuthorized Exception
UNAUTHORIZED_JWT_EXCEPTION("U001", "JWT 토큰이 유효하지 않습니다. 다시 로그인 해주세요."),
Expand Down

0 comments on commit 2cee355

Please sign in to comment.