Skip to content

Commit

Permalink
feat: 토큰 생성 및 화이트리스트에 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Jul 25, 2024
1 parent f07e88d commit b540dc2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/com/j9/bestmoments/service/Token_Service.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package com.j9.bestmoments.service;

import com.j9.bestmoments.domain.AccessToken;
import com.j9.bestmoments.domain.Member;
import com.j9.bestmoments.domain.RefreshToken;
import com.j9.bestmoments.domain.Token;
import com.j9.bestmoments.domain.Token_;
import com.j9.bestmoments.dto.response.LoginDto;
import com.j9.bestmoments.jwt.JwtTokenProvider;
import com.j9.bestmoments.repository.Token_Repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
public class Token_Service {

private final Token_Repository tokenRepository;
private final JwtTokenProvider jwtTokenProvider;

@Transactional
public LoginDto create(Member member) {
String accessToken = createAccessToken(member);
String refreshToken = createRefreshToken(member);
log.info("토큰 발급됨\n - accessToken: {}\n - refreshToken: {}", accessToken, refreshToken);
return new LoginDto("Bearer", accessToken, refreshToken, member.getDeletedAt());
}

private String createAccessToken(Member member) {
String accessTokenValue = jwtTokenProvider.generateAccessToken(member);
AccessToken accessToken = new AccessToken(member, accessTokenValue);
tokenRepository.save(accessToken);
return accessTokenValue;
}

private String createRefreshToken(Member member) {
String refreshTokenValue = jwtTokenProvider.generateRefreshToken(member);
RefreshToken refreshToken = new RefreshToken(member, refreshTokenValue);
tokenRepository.save(refreshToken);
return refreshTokenValue;
}

}

0 comments on commit b540dc2

Please sign in to comment.