Skip to content

Commit

Permalink
more data in token
Browse files Browse the repository at this point in the history
  • Loading branch information
melod1n committed Jun 12, 2024
1 parent 0606d78 commit 50b8d2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/main/kotlin/com/meloda/kubsau/plugins/Authentication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ fun Application.configureAuthentication() {
)

validate { credential ->
credential.payload.getClaim("id").asInt()?.let { userId ->
userDao.singleUser(userId)?.let { id ->
try {
UserPrincipal(
user = id,
type = credential.payload.getClaim("type").asInt(),
facultyId = credential.payload.getClaim("facultyId")?.asInt(),
departmentId = credential.payload.getClaim("departmentId")?.asInt()
)
} catch (e: Exception) {
null
credential.payload.run {
getClaim("id")?.asInt()?.let { userId ->
userDao.singleUser(userId)?.let { id ->
try {
UserPrincipal(
user = id,
type = getClaim("type").asInt(),
facultyId = getClaim("facultyId")?.asInt(),
selectedDepartmentId = getClaim("selectedDepartmentId")?.asInt(),
departmentIds = getClaim("departmentIds").asString().split(", ")
.mapNotNull { it.trim().toIntOrNull() }
)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
}
Expand All @@ -57,5 +62,6 @@ data class UserPrincipal(
val user: User,
val type: Int,
val facultyId: Int?,
val departmentId: Int?,
val selectedDepartmentId: Int?,
val departmentIds: List<Int>
) : Principal
4 changes: 3 additions & 1 deletion src/main/kotlin/com/meloda/kubsau/route/auth/AuthRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private fun Route.addSession() {
.withClaim("id", user.id)
.withClaim("type", employee.type)
.withClaim("facultyId", facultyId)
.withClaim("departmentIds", departmentIds.joinToString())
.sign(Algorithm.HMAC256(SecretsController.jwtSecret))

respondSuccess {
Expand Down Expand Up @@ -108,7 +109,8 @@ private fun Route.modifySession() {
.withClaim("id", user.id)
.withClaim("type", principal.type)
.withClaim("facultyId", principal.facultyId)
.withClaim("departmentId", departmentId)
.withClaim("selectedDepartmentId", departmentId)
.withClaim("departmentIds", principal.departmentIds.joinToString())
.sign(Algorithm.HMAC256(SecretsController.jwtSecret))

respondSuccess {
Expand Down

0 comments on commit 50b8d2c

Please sign in to comment.