Skip to content

Commit

Permalink
Merge pull request #76 from Team-Shaka/bug/75
Browse files Browse the repository at this point in the history
issue/75 ๐Ÿ› Fix : accessToken ์žฌ๋ฐœ๊ธ‰ ๋ฒ„๊ทธ ์ˆ˜์ •
  • Loading branch information
swa07016 authored Oct 14, 2023
2 parents d2a0d7f + 10cfd9e commit ce2e769
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/briefing/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum ErrorCode {

// member ๊ด€๋ จ ์—๋Ÿฌ

MEMBER_NOT_FOUND(BAD_REQUEST, "MEMBER_400_1", "์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค"),
MEMBER_NOT_FOUND(BAD_REQUEST, "MEMBER_001", "์‚ฌ์šฉ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค"),
MEMBER_NOT_SAME(BAD_REQUEST, "MEMBER_002", "๋กœ๊ทธ์ธ ๋œ ์‚ฌ์šฉ์ž์™€ ๋Œ€์ƒ ์‚ฌ์šฉ์ž๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),

// member ์—๋Ÿฌ
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/briefing/member/api/MemberApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -32,6 +36,9 @@
@Validated
@RequestMapping("/members")
@RequiredArgsConstructor
@ApiResponses({
@ApiResponse(responseCode = "COMMON000", description = "SERVER ERROR, ๋ฐฑ์•ค๋“œ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ์•Œ๋ ค์ฃผ์„ธ์š”", content = @Content(schema = @Schema(implementation = CommonResponse.class))),
})
public class MemberApi {
private final MemberQueryService memberQueryService;
private final MemberCommandService memberCommandService;
Expand Down Expand Up @@ -62,20 +69,35 @@ public CommonResponse<MemberResponse.LoginDTO> login(
return CommonResponse.onSuccess(MemberConverter.toLoginDTO(member, accessToken, refreshToken));
}

@Operation(summary = "02-01 Member\uD83D\uDC64 accessToken ์žฌ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ", description = "accessToken ๋งŒ๋ฃŒ ์‹œ refreshToken์œผ๋กœ ์žฌ๋ฐœ๊ธ‰์„ ๋ฐ›๋Š” API ์ž…๋‹ˆ๋‹ค.")
@ApiResponses({
@ApiResponse(responseCode = "1000",description = "OK, ์„ฑ๊ณต"),
@ApiResponse(responseCode = "COMMON001", description = "request body์— ๋‹ด๊ธธ ๊ฐ’์ด ์ด์ƒํ•จ, result๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”!",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "AUTH005", description = "๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ๋„ ๋งŒ๋ฃŒ, ๋‹ค์‹œ ๋กœ๊ทธ์ธ",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "AUTH009", description = "๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ๋ชจ์–‘์ด ์ž˜๋ชป ๋จ",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
})
@PostMapping("/auth/token")
public CommonResponse<MemberResponse.ReIssueTokenDTO> reissueToken(@Valid @RequestBody MemberRequest.ReissueDTO request){
RefreshToken refreshToken = redisService.reGenerateRefreshToken(request);
Member parsedMember = memberCommandService.parseRefreshToken(refreshToken);
String accessToken = tokenProvider.createAccessToken(parsedMember.getId(),parsedMember.getSocialType().toString(), parsedMember.getSocialId(), List.of(new SimpleGrantedAuthority(parsedMember.getRole().toString())));
return CommonResponse.onSuccess(MemberConverter.toReIssueTokenDTO(accessToken,refreshToken.getToken()));
return CommonResponse.onSuccess(MemberConverter.toReIssueTokenDTO(parsedMember.getId(), accessToken,refreshToken.getToken()));
}


@Operation(summary = "02-01 Member\uD83D\uDC64 ํšŒ์› ํƒˆํ‡ด", description = "ํšŒ์› ํƒˆํ‡ด API ์ž…๋‹ˆ๋‹ค.")
@DeleteMapping("/{memberId}")
@Parameters({
@Parameter(name = "member", hidden = true),
@Parameter(name = "memberId", description = "์‚ญ์ œ ๋Œ€์ƒ ๋ฉค๋ฒ„์•„์ด๋””")
})
@ApiResponses({
@ApiResponse(responseCode = "1000",description = "OK, ์„ฑ๊ณต"),
@ApiResponse(responseCode = "AUTH003", description = "access ํ† ํฐ์„ ์ฃผ์„ธ์š”!",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "AUTH004", description = "acess ํ† ํฐ ๋งŒ๋ฃŒ",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "AUTH006", description = "acess ํ† ํฐ ๋ชจ์–‘์ด ์ด์ƒํ•จ",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "MEMBER_001", description = "์‚ฌ์šฉ์ž๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
@ApiResponse(responseCode = "MEMBER_002", description = "๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž์™€ ํƒˆํ‡ด ๋Œ€์ƒ์ด ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.",content = @Content(schema = @Schema(implementation = CommonResponse.class))),
})
public CommonResponse<MemberResponse.QuitDTO> quitMember(@AuthMember Member member, @CheckSameMember @PathVariable Long memberId){
memberCommandService.deleteMember(memberId);
return CommonResponse.onSuccess(MemberConverter.toQuitDTO());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/briefing/member/api/MemberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public static Member toMember(String appleSocialId) {
.build();
}

public static MemberResponse.ReIssueTokenDTO toReIssueTokenDTO(String accessToken, String refreshToken){
public static MemberResponse.ReIssueTokenDTO toReIssueTokenDTO(Long memberId,String accessToken, String refreshToken){
return MemberResponse.ReIssueTokenDTO.builder()
.memberId(memberId)
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class LoginDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class ReIssueTokenDTO{
private Long memberId;
private String accessToken;
private String refreshToken;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/briefing/redis/domain/RefreshToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
public class RefreshToken {

@Id
private Long memberId;

private String token;

private Long memberId;

private LocalDateTime expireTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Optional;

public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> {
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, String> {

Optional<RefreshToken> findByToken(String token);
}
7 changes: 3 additions & 4 deletions src/main/java/briefing/redis/service/RedisServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public RefreshToken generateRefreshToken(String socialId, SocialType socialType)
public RefreshToken reGenerateRefreshToken(MemberRequest.ReissueDTO request) {
if(request.getRefreshToken() == null)
throw new MemberException(ErrorCode.INVALID_TOKEN_EXCEPTION);
RefreshToken findRefreshToken = refreshTokenRepository.findByToken(request.getRefreshToken()).orElseThrow(() -> new RefreshTokenException(ErrorCode.INVALID_TOKEN_EXCEPTION));
RefreshToken findRefreshToken = refreshTokenRepository.findById(request.getRefreshToken()).orElseThrow(() -> new RefreshTokenException(ErrorCode.INVALID_REFRESH_TOKEN));
LocalDateTime expireTime = findRefreshToken.getExpireTime();
LocalDateTime current = LocalDateTime.now();
// ํ…Œ์ŠคํŠธ์šฉ, ์‹ค์ œ๋กœ๋Š” ํ˜„์žฌ ์‹œ๊ฐ„ + accessToken ๋งŒ๋ฃŒ ์‹œ๊ฐ„
Expand All @@ -84,8 +84,7 @@ public RefreshToken reGenerateRefreshToken(MemberRequest.ReissueDTO request) {

@Override
public void deleteRefreshToken(String refreshToken) {
Optional<RefreshToken> target = refreshTokenRepository.findByToken(refreshToken);
if(target.isPresent())
refreshTokenRepository.delete(target.get());
Optional<RefreshToken> target = refreshTokenRepository.findById(refreshToken);
target.ifPresent(refreshTokenRepository::delete);
}
}

0 comments on commit ce2e769

Please sign in to comment.