diff --git a/src/main/java/briefing/exception/ErrorCode.java b/src/main/java/briefing/exception/ErrorCode.java index f95a337..d419d00 100644 --- a/src/main/java/briefing/exception/ErrorCode.java +++ b/src/main/java/briefing/exception/ErrorCode.java @@ -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 에러 diff --git a/src/main/java/briefing/member/api/MemberApi.java b/src/main/java/briefing/member/api/MemberApi.java index 630349a..87a448c 100644 --- a/src/main/java/briefing/member/api/MemberApi.java +++ b/src/main/java/briefing/member/api/MemberApi.java @@ -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; @@ -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; @@ -62,20 +69,35 @@ public CommonResponse 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 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 quitMember(@AuthMember Member member, @CheckSameMember @PathVariable Long memberId){ memberCommandService.deleteMember(memberId); return CommonResponse.onSuccess(MemberConverter.toQuitDTO()); diff --git a/src/main/java/briefing/member/api/MemberConverter.java b/src/main/java/briefing/member/api/MemberConverter.java index 58455a5..e327b07 100644 --- a/src/main/java/briefing/member/api/MemberConverter.java +++ b/src/main/java/briefing/member/api/MemberConverter.java @@ -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(); diff --git a/src/main/java/briefing/member/application/dto/MemberResponse.java b/src/main/java/briefing/member/application/dto/MemberResponse.java index 347aec0..8c976f8 100644 --- a/src/main/java/briefing/member/application/dto/MemberResponse.java +++ b/src/main/java/briefing/member/application/dto/MemberResponse.java @@ -23,6 +23,7 @@ public static class LoginDTO { @NoArgsConstructor @AllArgsConstructor public static class ReIssueTokenDTO{ + private Long memberId; private String accessToken; private String refreshToken; } diff --git a/src/main/java/briefing/redis/domain/RefreshToken.java b/src/main/java/briefing/redis/domain/RefreshToken.java index 94be74b..a94a493 100644 --- a/src/main/java/briefing/redis/domain/RefreshToken.java +++ b/src/main/java/briefing/redis/domain/RefreshToken.java @@ -14,9 +14,9 @@ public class RefreshToken { @Id - private Long memberId; - private String token; + private Long memberId; + private LocalDateTime expireTime; } diff --git a/src/main/java/briefing/redis/repository/RefreshTokenRepository.java b/src/main/java/briefing/redis/repository/RefreshTokenRepository.java index 75a87b6..b426170 100644 --- a/src/main/java/briefing/redis/repository/RefreshTokenRepository.java +++ b/src/main/java/briefing/redis/repository/RefreshTokenRepository.java @@ -5,7 +5,6 @@ import java.util.Optional; -public interface RefreshTokenRepository extends CrudRepository { +public interface RefreshTokenRepository extends CrudRepository { - Optional findByToken(String token); } diff --git a/src/main/java/briefing/redis/service/RedisServiceImpl.java b/src/main/java/briefing/redis/service/RedisServiceImpl.java index 04aa7dd..fb32967 100644 --- a/src/main/java/briefing/redis/service/RedisServiceImpl.java +++ b/src/main/java/briefing/redis/service/RedisServiceImpl.java @@ -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 만료 시간 @@ -84,8 +84,7 @@ public RefreshToken reGenerateRefreshToken(MemberRequest.ReissueDTO request) { @Override public void deleteRefreshToken(String refreshToken) { - Optional target = refreshTokenRepository.findByToken(refreshToken); - if(target.isPresent()) - refreshTokenRepository.delete(target.get()); + Optional target = refreshTokenRepository.findById(refreshToken); + target.ifPresent(refreshTokenRepository::delete); } }