init
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package space.luminic.budgerapp.services
|
||||
import org.springframework.cache.annotation.CacheEvict
|
||||
import org.springframework.cache.annotation.Cacheable
|
||||
import org.springframework.stereotype.Service
|
||||
import reactor.core.publisher.Mono
|
||||
import space.luminic.budgerapp.models.Token
|
||||
import space.luminic.budgerapp.models.TokenStatus
|
||||
import space.luminic.budgerapp.repos.TokenRepo
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class TokenService(private val tokenRepository: TokenRepo) {
|
||||
|
||||
@CacheEvict("tokens", allEntries = true)
|
||||
fun saveToken(token: String, username: String, expiresAt: LocalDateTime) {
|
||||
val newToken = Token(
|
||||
token = token,
|
||||
username = username,
|
||||
issuedAt = LocalDateTime.now(),
|
||||
expiresAt = expiresAt
|
||||
)
|
||||
tokenRepository.save(newToken)
|
||||
}
|
||||
|
||||
@CacheEvict("tokens", allEntries = true)
|
||||
fun revokeToken(token: String): Mono<Void> {
|
||||
return tokenRepository.findByToken(token)
|
||||
.flatMap { existingToken ->
|
||||
val updatedToken = existingToken.copy(status = TokenStatus.REVOKED)
|
||||
tokenRepository.save(updatedToken).then()
|
||||
}
|
||||
.switchIfEmpty(Mono.error(Exception("Token not found")))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@CacheEvict("tokens", allEntries = true)
|
||||
fun deleteExpiredTokens() {
|
||||
tokenRepository.deleteByExpiresAtBefore(LocalDateTime.now())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user