Skip to content

Commit

Permalink
hotfix : spotlessApply gradle 오류
Browse files Browse the repository at this point in the history
  • Loading branch information
jifrozen0110 committed Feb 21, 2024
1 parent 9c9eaa4 commit 904c93b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.compono.ibackend.auth.dto.response;

public record AuthRefreshResponse(
Long id,
String email
) {
public record AuthRefreshResponse(Long id, String email) {
public static AuthRefreshResponse of(Long userId, String email) {
return new AuthRefreshResponse(userId, email);
}
Expand Down
87 changes: 46 additions & 41 deletions src/main/java/com/compono/ibackend/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,59 @@
@RequiredArgsConstructor
public class AuthService {

private final JwtProvider jwtProvider;
private final UserRepository userRepository;
private static final String REFRESH_TOKEN_NAME = "refresh_token";
private static final String JWT_PREFIX = "Bearer ";
private final JwtProvider jwtProvider;
private final UserRepository userRepository;
private static final String REFRESH_TOKEN_NAME = "refresh_token";
private static final String JWT_PREFIX = "Bearer ";

public AuthRefreshResponse refresh(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
String refreshToken = getCookieFromHttpServletRequest(httpServletRequest);
assert refreshToken != null;
public AuthRefreshResponse refresh(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
String refreshToken = getCookieFromHttpServletRequest(httpServletRequest);
assert refreshToken != null;

Claims claims = jwtProvider.getClaims(refreshToken);
String email = claims.getSubject();
Claims claims = jwtProvider.getClaims(refreshToken);
String email = claims.getSubject();

User user =
userRepository
.findByEmail(email)
.orElseThrow(
() ->
new CustomException(
HttpStatus.BAD_REQUEST,
ErrorCode.NOT_FOUND_USER_ID));
User user =
userRepository
.findByEmail(email)
.orElseThrow(
() ->
new CustomException(
HttpStatus.BAD_REQUEST,
ErrorCode.NOT_FOUND_USER_ID));

Date now = Date.from(Instant.now());
if (claims.getExpiration().before(now)) {
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.COOKIE_EXPIRATION);
}

Long userId = user.getId();
String newAccessTokenValue = jwtProvider.createAccessToken(user.getEmail());
String newAccessToken = JWT_PREFIX + newAccessTokenValue;
httpServletResponse.setHeader(HttpHeaders.AUTHORIZATION, newAccessToken);
Date now = Date.from(Instant.now());
if (claims.getExpiration().before(now)) {
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.COOKIE_EXPIRATION);
}

return AuthRefreshResponse.of(userId, email);
}
Long userId = user.getId();
String newAccessTokenValue = jwtProvider.createAccessToken(user.getEmail());
String newAccessToken = JWT_PREFIX + newAccessTokenValue;
httpServletResponse.setHeader(HttpHeaders.AUTHORIZATION, newAccessToken);

public Claims getClaims(String accessToken) {
return jwtProvider.getClaims(accessToken);
}
return AuthRefreshResponse.of(userId, email);
}

private String getCookieFromHttpServletRequest(HttpServletRequest httpServletRequest) {
if (httpServletRequest.getCookies() == null) {
throw new CustomException(HttpStatus.UNAUTHORIZED, ErrorCode.NOT_EXIST_COOKIE);
public Claims getClaims(String accessToken) {
return jwtProvider.getClaims(accessToken);
}

Cookie cookie = Arrays.stream(httpServletRequest.getCookies())
.filter(c -> c.getName().equals(REFRESH_TOKEN_NAME))
.findFirst()
.orElseThrow(() -> new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NOT_EXIST_COOKIE));
return cookie.getValue();
}
private String getCookieFromHttpServletRequest(HttpServletRequest httpServletRequest) {
if (httpServletRequest.getCookies() == null) {
throw new CustomException(HttpStatus.UNAUTHORIZED, ErrorCode.NOT_EXIST_COOKIE);
}

Cookie cookie =
Arrays.stream(httpServletRequest.getCookies())
.filter(c -> c.getName().equals(REFRESH_TOKEN_NAME))
.findFirst()
.orElseThrow(
() ->
new CustomException(
HttpStatus.BAD_REQUEST,
ErrorCode.NOT_EXIST_COOKIE));
return cookie.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
public class TimestampConverter implements AttributeConverter<LocalDateTime, Date> {
@Override
public Date convertToDatabaseColumn(LocalDateTime attribute) {
return attribute == null ? null : Date.from(attribute.atZone(ZoneId.systemDefault()).toInstant()) ;
return attribute == null
? null
: Date.from(attribute.atZone(ZoneId.systemDefault()).toInstant());
}

@Override
public LocalDateTime convertToEntityAttribute(Date dbData) {
return dbData == null ? null : LocalDateTime.ofInstant(Instant.ofEpochMilli(dbData.getTime()), ZoneId.systemDefault());
return dbData == null
? null
: LocalDateTime.ofInstant(
Instant.ofEpochMilli(dbData.getTime()), ZoneId.systemDefault());
}
}

0 comments on commit 904c93b

Please sign in to comment.