Skip to content

Commit

Permalink
⚡️ :: 토큰 재발급 api
Browse files Browse the repository at this point in the history
  • Loading branch information
jyk1029 committed Nov 28, 2023
1 parent 72c8058 commit aad86e1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ class RefreshToken(

@Indexed
@field:NotBlank
val token: String
)
var token: String
) {
fun updateToken(token: String) {
this.token = token
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

@Repository
interface RefreshTokenRepository : CrudRepository<RefreshToken, String>
interface RefreshTokenRepository : CrudRepository<RefreshToken, String> {
fun findByToken(refreshToken: String): RefreshToken
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.example.sharing.domain.user.service.UpdateUserInfoService
import com.example.sharing.domain.user.service.UploadProfileService
import com.example.sharing.domain.user.service.UserLoginService
import com.example.sharing.domain.user.service.UserSignUpService
import com.example.sharing.domain.user.service.UserTokenRefreshService
import org.springframework.http.HttpStatus.*
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand All @@ -27,6 +28,7 @@ class UserController(
private val queryUserService: QueryUserService,
private val updateUserInfoService: UpdateUserInfoService,
private val uploadProfileService: UploadProfileService,
private val tokenRefreshService: UserTokenRefreshService,
) {
@ResponseStatus(CREATED)
@PostMapping("/signup")
Expand Down Expand Up @@ -56,4 +58,9 @@ class UserController(
fun uploadProfile(@RequestPart(name = "profile") file: MultipartFile): UploadProfileResponse {
return uploadProfileService.execute(file)
}

@PutMapping("/reissue")
fun tokenReissue(@RequestHeader("Authorization") refreshToken: String): TokenResponse {
return tokenRefreshService.execute(refreshToken)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.sharing.domain.user.service

import com.example.sharing.domain.user.domain.repository.RefreshTokenRepository
import com.example.sharing.domain.user.facade.UserFacade
import com.example.sharing.domain.user.presentation.dto.response.TokenResponse
import com.example.sharing.global.security.jwt.JwtTokenProvider
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class UserTokenRefreshService(
private val refreshTokenRepository: RefreshTokenRepository,
private val userFacade: UserFacade,
private val jwtTokenProvider: JwtTokenProvider,
) {
@Transactional
fun execute(refreshToken: String): TokenResponse {
val user = userFacade.getCurrentUser()
val token = refreshTokenRepository.findByToken(refreshToken)
val tokenResponse = jwtTokenProvider.getToken(user.accountId)
token.updateToken(tokenResponse.refreshToken)
return TokenResponse(tokenResponse.accessToken, tokenResponse.refreshToken)
}
}

0 comments on commit aad86e1

Please sign in to comment.