Skip to content

Commit 609618e

Browse files
committed
refactor : (#31) logout service
1 parent 39411c5 commit 609618e

File tree

1 file changed

+36
-0
lines changed
  • src/main/kotlin/entry/dsm/gitauth/equusgithubauth/domain/auth/service

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package entry.dsm.gitauth.equusgithubauth.domain.auth.service
2+
3+
import entry.dsm.gitauth.equusgithubauth.domain.auth.exception.InvalidAccessTokenException
4+
import entry.dsm.gitauth.equusgithubauth.domain.user.entity.repository.RefreshTokenRepository
5+
import entry.dsm.gitauth.equusgithubauth.global.security.jwt.JwtTokenProvider
6+
import org.springframework.stereotype.Service
7+
import org.springframework.transaction.annotation.Transactional
8+
import org.springframework.data.redis.core.RedisTemplate
9+
import java.util.concurrent.TimeUnit
10+
11+
@Service
12+
class LogoutService(
13+
private val jwtTokenProvider: JwtTokenProvider,
14+
private val redisTemplate: RedisTemplate<String, String>,
15+
private val refreshTokenRepository: RefreshTokenRepository
16+
) {
17+
18+
@Transactional
19+
fun logout(accessToken: String) {
20+
if (!jwtTokenProvider.valid(accessToken)) {
21+
throw InvalidAccessTokenException
22+
}
23+
24+
val userName = jwtTokenProvider.getSubjectFromToken(accessToken)
25+
26+
refreshTokenRepository.findByLoginId(userName)?.let {
27+
refreshTokenRepository.delete(it)
28+
}
29+
30+
val expiration = jwtTokenProvider.getExpiration(accessToken)
31+
if (expiration > 0) {
32+
redisTemplate.opsForValue().set("blacklist:$accessToken", "logout", expiration, TimeUnit.MILLISECONDS)
33+
}
34+
}
35+
}
36+

0 commit comments

Comments
 (0)