Hi, I’ve made a simple rest api & website with a logjn/register system in kotlin that mostly works, but I’m having a problem with one thing. After successful registration I want to return an access token and a refresh token to the client through headers and it seems that the client gets these in the headers as I can see them in the browser console, but I can’t access them in code, as there seems to be no headers? That’s a bit weird and I don’t know what to do with it.
As you can see here, the headers are there, but in the console they’re not being printed. Even when using a javascript command to print the same thing, there are no headers. Why is that?
Here’s the client code:
button {
+"Register"
onClick = {
if (!awaitingResponse) {
scope.launch {
awaitingResponse = true
val result: HttpResponse = register(providedUsername, providedPassword)
// Log all headers first to see what's available
result.headers.forEach { name, value ->
console.log("Header: $name = $value")
}
console.log(result.headers)
val authToken = result.headers["auth-token"]
val refreshToken = result.headers["refresh-token"]
console.log("Auth-Token: $authToken")
console.log("Refresh-Token: $refreshToken")
if (authToken != null && refreshToken != null) {
saveTokens(authToken, refreshToken)
}
awaitingResponse = false
}
}
}
}
val jsonClient = HttpClient {
install(ContentNegotiation) {
json()
}
}
suspend fun register(username: String, password: String): HttpResponse {
return jsonClient.post("http://localhost:8080/api/user/register") {
contentType(ContentType.Application.Json)
setBody(UserRequest(username, password))
}
}
and here’s the server code:
post("/register") {
val userRequest = call.receive<UserRequest>()
val createdUser = userService.save(user = userRequest.toModel()) ?: return@post call.respond(HttpStatusCode.BadRequest)
call.response.header(name = "id", value = createdUser.id.toString())
call.response.header(name = "auth-token", value = jwtService.createAccessToken(createdUser.username))
call.response.header(name = "refresh-token", value = jwtService.createRefreshToken(createdUser.username))
logger.info(call.response.headers["auth-token"])
logger.info(call.response.headers["refresh-token"])
call.respond(message = HttpStatusCode.Created)
}