Adds isAdmin to JWT
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2026-05-16 16:14:29 -05:00
parent ec2bb1bcc6
commit 877e134166
3 changed files with 7 additions and 4 deletions

View File

@@ -27,9 +27,10 @@ class JwtUtil {
}
// Generate JWT token
fun generateToken(email: String): String {
fun generateToken(email: String, isAdmin: Boolean): String {
return Jwts.builder()
.subject(email)
.claim("isAdmin", isAdmin)
.issuedAt(Date())
.expiration(Date(System.currentTimeMillis() + jwtExpirationMs))
.signWith(key)

View File

@@ -24,7 +24,7 @@ class AuthController(private val loginService: LoginService, private val jwtUtil
@PostMapping("/login")
fun login(@Valid @RequestBody body: LoginRequest): ResponseEntity<LoginResponse> {
val result = loginService.login(body.toDomain())
val accessToken = jwtUtils.generateToken(result.email)
val accessToken = jwtUtils.generateToken(result.email, result.isAdmin)
val refreshToken = refreshTokenService.generateRefreshToken(result.email)
val loginResponse = LoginResponse(accessToken, refreshToken, result.name)
return ResponseEntity.ok(loginResponse)

View File

@@ -5,6 +5,7 @@ import net.halfbinary.scavengerhuntapi.error.exception.ExpiredRefreshTokenExcept
import net.halfbinary.scavengerhuntapi.error.exception.InvalidRefreshTokenException
import net.halfbinary.scavengerhuntapi.model.RefreshId
import net.halfbinary.scavengerhuntapi.model.record.RefreshTokenRecord
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
import net.halfbinary.scavengerhuntapi.repository.RefreshTokenRepository
import org.slf4j.LoggerFactory
import org.springframework.data.repository.findByIdOrNull
@@ -13,7 +14,7 @@ import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
@Service
class RefreshTokenService(private val refreshTokenRepository: RefreshTokenRepository, private val jwtUtil: JwtUtil) {
class RefreshTokenService(private val refreshTokenRepository: RefreshTokenRepository, private val jwtUtil: JwtUtil, private val hunterRepository: HunterRepository) {
companion object {
private val log = LoggerFactory.getLogger(RefreshTokenService::class.java)
@@ -25,7 +26,8 @@ class RefreshTokenService(private val refreshTokenRepository: RefreshTokenReposi
removeToken(tokenId)
throw ExpiredRefreshTokenException(tokenId)
} else {
jwtUtil.generateToken(refreshToken.email)
val isAdmin = hunterRepository.findByEmail(refreshToken.email)?.isAdmin ?: false
jwtUtil.generateToken(refreshToken.email, isAdmin)
}
}?: throw InvalidRefreshTokenException(tokenId)
}