Skip to content

Commit

Permalink
refactor : 안드로이드에 맞도록 accessToken으로 회원가입/로그인 처리 (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwnsdnjs13 authored Jul 20, 2024
1 parent 9b68455 commit 20b11af
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ public class MemberController {
public JwtTokenResponse create(@RequestBody @Valid RegisterReq request) {

Member member = request.toDomain();
Member memberResult = memberUsecase.create(member);
Member memberResult = memberUsecase.create(request.accessToken(), member);

return new JwtTokenResponse(jwtTokenUtil.getJWTToken(memberResult));
}

@GetMapping("/{idCode}")
@GetMapping("/{accessToken}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Member 로그인 API")
public JwtTokenResponse login(
@PathVariable("idCode")
@Parameter(name = "idCode", description = "sns idCode", required = true)
String idCode) {
@PathVariable("accessToken")
@Parameter(
name = "accessToken",
description = "sns accessToken",
required = true)
String accessToken) {

Member member = memberUsecase.login(idCode);
Member member = memberUsecase.login(accessToken);

return new JwtTokenResponse(jwtTokenUtil.getJWTToken(member));
}
Expand All @@ -67,4 +70,14 @@ public Boolean duplicatedNickname(
Boolean result = memberUsecase.duplicatedNickname(nickname);
return result;
}

@GetMapping("/accessToken/{idCode}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "(백엔드용)accessToken을 받아오기 위한 API")
public String getAccessToken(
@PathVariable("idCode")
@Parameter(name = "idCode", description = "카카오에서 발급 받은 idCode", required = true)
String idCode) {
return memberUsecase.getAccessToken(idCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import io.swagger.v3.oas.annotations.media.Schema;

public record RegisterReq(
@NotNull(message = "인가 id code는 필수 값입니다.") @Schema(description = "인가 id code")
String idCode,
@NotNull(message = "인가 accessToken는 필수 값입니다.") @Schema(description = "카카오 인증 accessToken")
String accessToken,
@NotNull(message = "닉네임 값은 필수입니다.")
@Schema(description = "설정하려는 닉네임")
@Length(min = 2, max = 10, message = "닉네임은 2글자에서 10글자 사이여야합니다.")
Expand All @@ -25,6 +25,6 @@ public record RegisterReq(
Long teamId) {

public Member toDomain() {
return Member.builder().idToken(idCode).nickname(nickname).teamId(teamId).build();
return Member.builder().nickname(nickname).teamId(teamId).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public Member getLoginUserInfo(String accesstoken) {

// TODO : idToken이 변경 될 수 있음. 등록된 email도 변경될 수 있기에 추 후 논의가 필요.
// 기존 유저와 비교를 위해선 idToken만 필요함.
// 앱에서는 accessToken을 반환해주기에 accessToken으로 로직 처리
return userInfo.toLoginDomain();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public interface MemberUsecase {

Member create(Member member);
Member create(String accessToken, Member member);

Member login(String idCode);

Boolean duplicatedNickname(String nickname);

String getAccessToken(String idCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public class MemberService implements MemberUsecase {
private final MemberRepository memberRepository;

@Override
public Member create(Member member) {
public Member create(String accessToken, Member member) {
if (memberRepository.existsByNickname(member.getNickname())) {
throw new MemberNicknameConflictException();
}
String accessToken = oauthRepository.getKakaoAccessToken(member.getIdToken());
Member memberResult = oauthRepository.getRegisterUserInfo(accessToken, member);
Optional<Member> existedMember = memberRepository.findByIdToken(memberResult.getIdToken());
if (existedMember.isPresent()) {
Expand All @@ -36,8 +35,7 @@ public Member create(Member member) {
}

@Override
public Member login(String idCode) {
String accessToken = oauthRepository.getKakaoAccessToken(idCode);
public Member login(String accessToken) {
Member memberResult = oauthRepository.getLoginUserInfo(accessToken);
Optional<Member> existedMember = memberRepository.findByIdToken(memberResult.getIdToken());
if (existedMember.isEmpty()) {
Expand All @@ -52,4 +50,9 @@ public Boolean duplicatedNickname(String nickname) {
throw new MemberNicknameConflictException();
return Boolean.FALSE;
}

@Override
public String getAccessToken(String idCode) {
return oauthRepository.getKakaoAccessToken(idCode);
}
}

0 comments on commit 20b11af

Please sign in to comment.