Skip to content

Commit

Permalink
feat: Add CoupleIdPath validate
Browse files Browse the repository at this point in the history
  • Loading branch information
jinsu4755 committed Nov 21, 2023
1 parent b22fef9 commit 4f59226
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static java.util.Objects.*;

import com.universe.uni.domain.entity.User;
import com.universe.uni.exception.ApiException;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.repository.UserRepository;
import java.io.IOException;

import javax.servlet.FilterChain;
Expand All @@ -28,6 +32,7 @@
public class JwtAuthenticationFilter extends OncePerRequestFilter {

private final JwtManager jwtManager;
private final UserRepository userRepository;

@Override
protected void doFilterInternal(
Expand All @@ -39,6 +44,13 @@ protected void doFilterInternal(
if (isContainApiPath(uri)) {
String token = getJwtFromRequest(request);
Long userId = jwtManager.getUserIdFromJwt(token);
// 임시로 커플아이디 확인 로직 추가
if (!isNotRequiredCoupleIdPath(uri)) {
User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(ErrorType.USER_NOT_EXISTENT));
if (user.getCouple() == null) {
throw new NotFoundException(ErrorType.COUPLE_NOT_EXISTENT);
}
}
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(userId, null, null);
SecurityContextHolder.getContext().setAuthentication(authentication);
Expand All @@ -62,4 +74,8 @@ private String getJwtFromRequest(HttpServletRequest request) {
private boolean isContainApiPath(String uri) {
return uri.startsWith("/api");
}

private boolean isNotRequiredCoupleIdPath(String uri) {
return uri.contains("user");
}
}

0 comments on commit 4f59226

Please sign in to comment.