Skip to content

Commit

Permalink
fix: 토큰 만료시간 8시간으로 변경, 토큰을 Body에 담아 응답하도록 변경 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Feb 6, 2024
1 parent 2c65080 commit 976bfc0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.listywave.auth.application.domain;

import static com.listywave.common.exception.ErrorCode.REQUIRED_ACCESS_TOKEN;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.HOURS;

import com.listywave.common.exception.CustomException;
import io.jsonwebtoken.Jwts;
Expand All @@ -14,7 +14,7 @@
public class JwtManager {

private static final SecretKey key = Jwts.SIG.HS256.key().build();
private static final Long ACCESS_TOKEN_VALID_MILLISECOND = MINUTES.toMillis(30);
private static final Long ACCESS_TOKEN_VALID_MILLISECOND = HOURS.toMillis(8);

public String createToken(Long userId) {
Date now = new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public record LoginResponse(
String description,
int followingCount,
int followerCount,
boolean isFirst
boolean isFirst,
String accessToken
) {

public static LoginResponse of(User user, boolean isFirst) {
public static LoginResponse of(User user, boolean isFirst, String accessToken) {
return new LoginResponse(
user.getId(),
user.getProfileImageUrl(),
Expand All @@ -22,7 +23,8 @@ public static LoginResponse of(User user, boolean isFirst) {
user.getDescription(),
user.getFollowingCount(),
user.getFollowerCount(),
isFirst
isFirst,
accessToken
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public LoginResponse login(String authCode) {
if (foundUser.isEmpty()) {
User user = User.initialCreate(kakaoMember.id(), kakaoMember.kakaoAccount().email());
User createdUser = userRepository.save(user);
return LoginResponse.of(createdUser, true);
return LoginResponse.of(createdUser, true, createTokenBy(createdUser.getId()));
}
return LoginResponse.of(foundUser.get(), false);
return LoginResponse.of(foundUser.get(), false, createTokenBy(foundUser.get().getId()));
}

public String createToken(Long userId) {
private String createTokenBy(Long userId) {
return jwtManager.createToken(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -21,21 +20,13 @@ public class AuthController {
@GetMapping("/auth/kakao")
ResponseEntity<Void> redirectAuthCodeRequestUrl(HttpServletResponse response) throws IOException {
String requestUrl = authService.provideRedirectUri();

response.sendRedirect(requestUrl);
return ResponseEntity.status(HttpStatus.FOUND).build();
}

@GetMapping("/auth/redirect/kakao")
ResponseEntity<LoginResponse> login(
@RequestParam(name = "code") String authCode,
HttpServletResponse response
) {
ResponseEntity<LoginResponse> login(@RequestParam(name = "code") String authCode) {
LoginResponse loginResponse = authService.login(authCode);

String accessToken = authService.createToken(loginResponse.id());

response.setHeader(HttpHeaders.SET_COOKIE, accessToken);
return ResponseEntity.ok(loginResponse);
}
}

0 comments on commit 976bfc0

Please sign in to comment.