From b540dc22547712bb56e6c3ee7bd8f4ddfd7fbb51 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Thu, 25 Jul 2024 20:41:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=86=A0=ED=81=B0=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EB=B0=8F=20=ED=99=94=EC=9D=B4=ED=8A=B8=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=97=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../j9/bestmoments/service/Token_Service.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/com/j9/bestmoments/service/Token_Service.java b/src/main/java/com/j9/bestmoments/service/Token_Service.java index ccbc9f2..2fac08b 100644 --- a/src/main/java/com/j9/bestmoments/service/Token_Service.java +++ b/src/main/java/com/j9/bestmoments/service/Token_Service.java @@ -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; + } }