Yoonho Aaron Kim
05/21/2025, 2:20 AMinternal fun Application.configureSession() {
install(Sessions) {
cookie<UserSession>("USER_SESSION") {
cookie.extensions["SameSite"] = "None"
cookie.secure = true
}
}
install(CORS) {
allowCredentials = true
allowSameOrigin = true
anyHost()
anyMethod()
allowHeader(HttpHeaders.ContentType)
allowHeader(HttpHeaders.Authorization)
allowHeader(HttpHeaders.AccessControlAllowOrigin)
}
}
Here are the client-side s ettings
actual val client: HttpClient = HttpClient(Js) {
engine {
}
install(DefaultRequest) {
headers {
append(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
install(ContentNegotiation) {
json(json = Json {
isLenient = true
ignoreUnknownKeys = true
})
}
install(Logging)
install(HttpCookies)
}
And below code is handling the google oauth in Ktor Server side:
authenticate(GOOGLE_AUTH) {
get("/login") { }
get("/callback") {
val tokenResponse = call.principal<OAuthAccessTokenResponse.OAuth2>()
if (tokenResponse == null) {
call.respondRedirect("/")
return@get
}
val state = tokenResponse.state
if (state.isNullOrBlank()) {
call.respondRedirect("/")
return@get
}
val token = tokenResponse.accessToken
val me = signInService.signIn(token)
call.sessions.set(UserSession(me.id)) // the session is saved and I can check it in my browser developer tool
println("[test] callback - session is saved: $me")
call.respondRedirect("/")
}
}
Somebody help me please?!Aleksei Tirman [JB]
05/21/2025, 8:31 AMYoonho Aaron Kim
05/21/2025, 11:23 PMCalls to 'js(code)' should be a single expression inside a top-level function body or a property initializer in Kotlin/Wasm
I looked it up, but... I didn't find a good solution.
We'll have to wait for Ktor 3.2.0 and see...
I'm going to deploy it to a production server and try to match the domain.Aleksei Tirman [JB]
05/22/2025, 7:49 AMfetch
function from the JS code.