Skip to content

Commit

Permalink
feat: 현재 로그인한 멤버 조회하는 유틸리티 구현 (#41)
Browse files Browse the repository at this point in the history
* feat: 멤버 유틸리티 구현

* refactor: 예외 케이스 분리 및 불필요한 메서드 제거
  • Loading branch information
uwoobeat authored Feb 11, 2024
1 parent 2cdb216 commit 9ee3af9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
public enum ErrorCode {
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."),

// Jwt
// Auth
INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 JWT 토큰입니다."),
EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 JWT 토큰입니다."),
AUTH_NOT_EXIST(HttpStatus.INTERNAL_SERVER_ERROR, "시큐리티 인증 정보가 존재하지 않습니다."),
AUTH_NOT_PARSABLE(HttpStatus.INTERNAL_SERVER_ERROR, "시큐리티 인증 정보 파싱에 실패했습니다."),

// Parameter
INVALID_QUERY_PARAMETER(HttpStatus.BAD_REQUEST, "잘못된 쿼리 파라미터입니다."),

// Member
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."),
MEMBER_DELETED(HttpStatus.CONFLICT, "탈퇴한 회원입니다.");
MEMBER_DELETED(HttpStatus.CONFLICT, "탈퇴한 회원입니다."),
;

private final HttpStatus status;
private final String message;
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/gdschongik/gdsc/global/util/MemberUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.gdschongik.gdsc.global.util;

import com.gdschongik.gdsc.domain.member.dao.MemberRepository;
import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MemberUtil {

private final MemberRepository memberRepository;

public Long getCurrentMemberId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

validateAuthenticationNotNull(authentication);

try {
return Long.parseLong(authentication.getName());
} catch (NumberFormatException e) {
throw new CustomException(ErrorCode.AUTH_NOT_PARSABLE);
}
}

private void validateAuthenticationNotNull(Authentication authentication) {
if (authentication == null) {
throw new CustomException(ErrorCode.AUTH_NOT_EXIST);
}
}

public Member getCurrentMember() {
return memberRepository
.findById(getCurrentMemberId())
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
}
}

0 comments on commit 9ee3af9

Please sign in to comment.