-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 멤버 유틸리티 구현 * refactor: 예외 케이스 분리 및 불필요한 메서드 제거
- Loading branch information
Showing
2 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/gdschongik/gdsc/global/util/MemberUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |