Skip to content

Commit

Permalink
feat: 토큰 재발급 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Nov 6, 2024
1 parent 8827ccd commit a81e310
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Backend CD # actions 이름

on:
push:
branches: [ develop ]
branches: [ feat/#47 ]

jobs:
deploy:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.pocket.domain.port.user;

import com.pocket.domain.dto.user.LoginResponse;
import com.pocket.domain.dto.user.UserInfoDTO;

public interface LoadUserInfoPort {

UserInfoDTO loadUserInfo(String name);

LoginResponse createToken(String email);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pocket.domain.service.user;

import com.pocket.domain.dto.user.LoginResponse;
import com.pocket.domain.dto.user.UserInfoDTO;
import com.pocket.domain.port.user.LoadUserInfoPort;
import com.pocket.domain.usecase.user.LoginUseCase;
Expand All @@ -15,4 +16,8 @@ public class UserQueryService implements LoginUseCase {
public UserInfoDTO getUserInfo(String name) {
return loadUserInfoPort.loadUserInfo(name);
}

public LoginResponse reissueToken(String email) {
return loadUserInfoPort.createToken(email);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.pocket.domain.usecase.user;

import com.pocket.domain.dto.user.LoginResponse;
import com.pocket.domain.dto.user.UserInfoDTO;

public interface LoginUseCase {

UserInfoDTO getUserInfo(String name);

LoginResponse reissueToken(String email);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.pocket.inbounds.user.presentation;

import com.pocket.core.aop.annotation.NameAuthenticated;
import com.pocket.core.exception.common.ApplicationResponse;
import com.pocket.domain.dto.user.LoginResponse;
import com.pocket.domain.dto.user.UserInfoDTO;
import com.pocket.domain.usecase.user.LoginUseCase;
import com.pocket.inbounds.user.response.UserResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -27,4 +28,12 @@ public ApplicationResponse<UserResponse> getUserInfo(
UserResponse response = new UserResponse(userInfoDTO.name(), userInfoDTO.email(), userInfoDTO.image());
return ApplicationResponse.ok(response);
}

@GetMapping(value = "/reissue", produces = MediaType.APPLICATION_JSON_VALUE)
public ApplicationResponse<LoginResponse> refresh(
@AuthenticationPrincipal UserInfoDTO userInfoDTO
) {
LoginResponse tokenResponse = loginUseCase.reissueToken(userInfoDTO.email());
return ApplicationResponse.ok(tokenResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface UserControllerDocs {
@Operation(summary = "사용자 정보 제공", description = "사용자 정보 전달 API")
ApplicationResponse<UserResponse> getUserInfo(
@AuthenticationPrincipal UserInfoDTO userInfoDTO);


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.pocket.core.exception.user.UserCustomException;
import com.pocket.core.exception.user.UserErrorCode;
import com.pocket.domain.dto.user.LoginResponse;
import com.pocket.domain.dto.user.UserInfoDTO;
import com.pocket.domain.port.user.LoadUserInfoPort;
import com.pocket.outbound.entity.JpaUser;
import com.pocket.outbound.repository.UserRepository;
import com.pocket.outbound.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -14,6 +16,7 @@
public class UserAdapter implements LoadUserInfoPort {

private final UserRepository userRepository;
private final JwtUtil jwtUtil;

public UserInfoDTO loadUserInfo(String name) {
JpaUser user = userRepository.findByUserName(name)
Expand All @@ -23,9 +26,23 @@ public UserInfoDTO loadUserInfo(String name) {
}

public UserInfoDTO loadUserInfoByEmail(String email) {
JpaUser user = userRepository.findByUserEmail(email)
.orElseThrow(() -> new UserCustomException(UserErrorCode.NO_USER_INFO));

final JpaUser user = findUser(email);
return new UserInfoDTO(user.getUser().getName(), user.getUser().getEmail(), user.getUser().getPicture());
}

public LoginResponse createToken(String email) {

final JpaUser user = findUser(email);

String accessToken = jwtUtil.createJwtAccessToken(user.getUser().getEmail(), user.getUser().getSubId());
String refreshToken = jwtUtil.createJwtRefreshToken(user.getUser().getEmail(), user.getUser().getSubId());

return new LoginResponse(accessToken, refreshToken);
}

private JpaUser findUser(String email) {
return userRepository.findByUserEmail(email)
.orElseThrow(() -> new UserCustomException(UserErrorCode.NO_USER_INFO));
}
}

0 comments on commit a81e310

Please sign in to comment.