Skip to content

Commit

Permalink
[refactor] 토큰 재발급 시, role 반환 추가 #68
Browse files Browse the repository at this point in the history
[refactor] 토큰 재발급 시, role 반환 추가
  • Loading branch information
yangchef1 authored Jul 1, 2024
2 parents 7f8bf2c + ee931cf commit 69776e2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sanbosillok.sanbosillokserver.api.auth.controller;

import com.sanbosillok.sanbosillokserver.api.auth.dto.RefreshTokenRequest;
import com.sanbosillok.sanbosillokserver.api.auth.dto.ReissueTokenResponse;
import com.sanbosillok.sanbosillokserver.api.auth.dto.TokenResponse;
import com.sanbosillok.sanbosillokserver.api.auth.service.TokenService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,7 +14,7 @@ public class TokenController {
private final TokenService tokenService;

@PostMapping("/refresh")
public ReissueTokenResponse getNewToken(@RequestBody @Valid RefreshTokenRequest refreshTokenRequest) {
public TokenResponse getNewToken(@RequestBody @Valid RefreshTokenRequest refreshTokenRequest) {
return tokenService.reIssueAccessToken(refreshTokenRequest.getRefreshToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

@Data
@AllArgsConstructor
public class ReissueTokenResponse {
public class TokenResponse {
private String accessToken;
private String refreshToken;
private String role;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sanbosillok.sanbosillokserver.api.auth.service;

import com.sanbosillok.sanbosillokserver.api.auth.dto.ReissueTokenResponse;
import com.sanbosillok.sanbosillokserver.api.auth.dto.TokenResponse;
import com.sanbosillok.sanbosillokserver.api.member.domain.Member;
import com.sanbosillok.sanbosillokserver.api.member.repository.MemberRepository;
import com.sanbosillok.sanbosillokserver.config.jwt.JwtTokenProvider;
Expand All @@ -13,7 +13,7 @@ public class TokenService {
private final JwtTokenProvider jwtTokenProvider;
private final MemberRepository memberRepository;

public ReissueTokenResponse reIssueAccessToken(String refreshToken) {
public TokenResponse reIssueAccessToken(String refreshToken) {

if (refreshToken == null) {
throw new IllegalArgumentException("토큰이 비어있습니다.");
Expand All @@ -30,6 +30,6 @@ public ReissueTokenResponse reIssueAccessToken(String refreshToken) {
String role = "ROLE_" + member.getRole().toString();
Long id = member.getId();

return new ReissueTokenResponse(jwtTokenProvider.createAccessToken(username, role), jwtTokenProvider.createRefreshToken(id));
return new TokenResponse(jwtTokenProvider.createAccessToken(username, role), jwtTokenProvider.createRefreshToken(id), role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanbosillok.sanbosillokserver.api.auth.dto.CustomUserDetails;
import com.sanbosillok.sanbosillokserver.api.auth.dto.LoginResponse;
import com.sanbosillok.sanbosillokserver.api.auth.dto.TokenResponse;
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
String accessToken = jwtTokenProvider.createAccessToken(username, role);

response.setContentType("application/json");
response.getWriter().print(objectMapper.writeValueAsString(new LoginResponse(refreshToken, accessToken, role)));
response.getWriter().print(objectMapper.writeValueAsString(new TokenResponse(refreshToken, accessToken, role)));
}

@Override
Expand Down

0 comments on commit 69776e2

Please sign in to comment.