Skip to content

Commit

Permalink
Merge pull request #144 from Gam-develop/fix/#143-listReturn-TokenError
Browse files Browse the repository at this point in the history
[CHORE] userTag 리턴값 "" to Null, Token 에러 catch
  • Loading branch information
GaHee99 authored Feb 16, 2024
2 parents 02463e3 + 7edcd9a commit eed1994
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum ExceptionMessage {
/** auth **/
SECURITY_FILTER_EXCPETION("시큐리티 예외"),
TOKEN_USER_EXCEPTION("토큰 에러 - 토큰에 해당하는 유저를 찾을 수 없습니다."),
WITHDRAWAL_USER("탈퇴한 사용자입니다."),
EMPTY_TOKEN("빈 토큰입니다."),
INVALID_TOKEN("유효하지 않은 토큰입니다."),
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/gam/api/config/jwt/JwtExceptionFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.gam.api.common.ApiResponse;
import com.gam.api.common.exception.AuthException;
import javax.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
Expand All @@ -26,11 +27,15 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest,
) throws ServletException, IOException {
try {
filterChain.doFilter(httpServletRequest, httpServletResponse);
} catch(AuthException e) {
} catch(AuthException | EntityNotFoundException e) {
val objectMapper = new ObjectMapper();
val jsonResponse = objectMapper.writeValueAsString(ApiResponse.fail(e.getMessage()));
String jsonResponse = objectMapper.writeValueAsString(ApiResponse.fail(e.getMessage()));

httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
if(e instanceof AuthException) {
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
} else if(e instanceof EntityNotFoundException) {
httpServletResponse.setStatus(HttpStatus.NOT_FOUND.value());
}
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.getWriter().write(jsonResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ record UserInfoVO(
public static UserInfoVO of(User user, boolean designerScrap){
return UserInfoVO.builder()
.userId(user.getId())
.userTag(user.getTags())
.userTag(user.getTags() == null ? new int[0] : user.getTags())
.userName(user.getUserName())
.viewCount(user.getViewCount())
.userDetail(user.getDetail())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static UserMyProfileResponseDTO of(User user){
.info(user.getInfo())
.detail(user.getDetail())
.email(user.getEmail())
.userTag(user.getTags())
.userTag(user.getTags() == null ? new int[0] : user.getTags())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gam.api.dto.user.response;

import com.gam.api.entity.User;
import java.util.ArrayList;
import lombok.Builder;

@Builder
Expand Down Expand Up @@ -32,7 +33,7 @@ public static UserProfileResponseVO of(User user){
.info(user.getInfo())
.detail(user.getDetail())
.email(user.getEmail())
.userTag(user.getTags())
.userTag(user.getTags() == null ? new int[0] : user.getTags())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static UserResponseDTO of (User user, boolean designerScrap){
.userName(user.getUserName())
.userWorkThumbNail(Objects.isNull(user.getWorkThumbNail()) ? "" : user.getWorkThumbNail())
.viewCount(user.getViewCount())
.userTag(user.getTags())
.userTag(user.getTags() == null ? new int[0] : user.getTags())
.designerScrap(designerScrap)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByUsername(String userId) {
List<GrantedAuthority> authorities = new ArrayList<>();
val user = userRepository.findById(Long.parseLong(userId))
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.NOT_FOUND_USER.getMessage()));
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.TOKEN_USER_EXCEPTION.getMessage()));

val userRole = user.getRole();
val userStatus = user.getUserStatus();

val authUser = authProviderRepository.searchAuthProviderByUser(user)
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.NOT_FOUND_USER.getMessage()));
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.TOKEN_USER_EXCEPTION.getMessage()));

val authUserId = String.valueOf(authUser.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public UserProfileResponseDTO getUserProfile(Long myId, Long userId) {
if(Objects.nonNull(userScrap)){
return UserProfileResponseDTO.of(userScrap.isStatus(), user);
}
System.out.println(user.getTags());
return UserProfileResponseDTO.of(false, user);
}

Expand Down

0 comments on commit eed1994

Please sign in to comment.