Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : pk 기반 사용자 조회 및 요청 사용자 정보 조회 메소드 추가 #14

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import core.kobaco.application.user.service.dto.response.UserLoginResponse;
import core.kobaco.domain.user.service.UserReader;
import core.kobaco.domain.user.service.UserValidator;
import core.kobaco.global.jwt.JwtFactory;
import core.kobaco.global.jwt.JwtProvider;
import core.kobaco.global.jwt.BearerTokenGenerator;
import core.kobaco.global.jwt.PrivateClaims;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -16,14 +15,14 @@
public class UserService {
private final UserReader userReader;
private final UserValidator userValidator;
private final JwtFactory jwtFactory;
private final BearerTokenGenerator bearerTokenGenerator;

@Transactional(readOnly = true)
public UserLoginResponse login(UserLoginRequest request){
if(userValidator.existsByEmailAndPassword(request.email(), request.password())){
PrivateClaims.UserClaims userClaims = PrivateClaims.UserClaims.of(userReader.read(request.email()).getId());
return new UserLoginResponse(jwtFactory.generateAccessToken(userClaims));
return new UserLoginResponse(bearerTokenGenerator.generateAccessTokenAndAttachBearer(userClaims));
}
throw new RuntimeException("user not found"); // TODO : 예외 정의하기
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface UserRepository {
Boolean existsByEmailAndPassword(String email, String password);
Optional<User> findByEmail(String email);

Optional<User> findById(Long id);

}
18 changes: 18 additions & 0 deletions kobaco/kobaco/src/main/java/core/kobaco/domain/user/UserUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.kobaco.domain.user;

import core.kobaco.domain.user.service.UserReader;
import core.kobaco.global.jwt.JwtUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserUtils {
private final JwtUtils jwtUtils;
private final UserReader userReader;

public User getRequestUser(){
final Long userId = jwtUtils.getRequestUserId();
return userReader.read(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public User read(String email){
return userRepository.findByEmail(email)
.orElseThrow(() -> new RuntimeException("user not found"));
}

public User read(Long id){
return userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("user not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@RequiredArgsConstructor
public class JwtFactory {
public class BearerTokenGenerator {
private final JwtProvider jwtProvider;

public String generateAccessToken(PrivateClaims.UserClaims userClaims){
public String generateAccessTokenAndAttachBearer(PrivateClaims.UserClaims userClaims){
return JwtHeaderConsts.BEARER_PREFIX + jwtProvider.generateAccessToken(userClaims);
}
}

public String detachBearerPrefix(String token){
return token.replace(JwtHeaderConsts.BEARER_PREFIX, "");
}

}
28 changes: 28 additions & 0 deletions kobaco/kobaco/src/main/java/core/kobaco/global/jwt/JwtUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.kobaco.global.jwt;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

@Component
@RequiredArgsConstructor
public class JwtUtils {
private final JwtProvider jwtProvider;
private final BearerTokenGenerator bearerTokenGenerator;

public PrivateClaims.UserClaims parseUserClaims() {
HttpServletRequest request =
((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
String tokenWithBearer = request.getHeader(JwtHeaderConsts.AUTHORIZATION_HEADER);
String token = bearerTokenGenerator.detachBearerPrefix(tokenWithBearer);
return jwtProvider.extractUserClaimsFromToken(token, TokenType.ACCESS_TOKEN);
}

public Long getRequestUserId(){
return parseUserClaims().getUserId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public Optional<User> findByEmail(String email) {
return userJpaRepository.findByEmail(email)
.map(userMapper::toDomain);
}

@Override
public Optional<User> findById(Long id) {
return userJpaRepository.findById(id)
.map(userMapper::toDomain);
}
}
Loading