So im trying to set up a OAuth way of logging in my Ktor program. Quite new to Kotlin and Ktor so I am having a hard time. I am able to create a token and I get sent to the "get("/callback")" part of the plugin generated code.
But as i try to run the line
call.sessions.set(UserSession(principal!!.state!!, principal.accessToken))
I get
"Session data for type `class com.example.plugins.UserSession` was not registered"
I have tried to understand what is going wrong but at this point I have no clue. UserSession wants two strings, but if i replace my two inputs with just
`UserSession("random string", "random string")`
I get the same error.
My values for principal are
accessToken = ya29.a0AVvZVsrXsX2_6eu-3N_1ITbT3i-7QIvytMtJvu0zU93NyPoWBPMM42k1Ypf9VQrsrK_FygNEAhwdfJUbSZem7_UQSBNupOaLbsm9NB0QJl6rZt3-0cmQMDvUVG_oIYl8rorpooyekPWMVPyMYQSfBpyjHnfeaCgYKAYESARESFQGbdwaIaU-pvIfQ3cEVeXbd-4jRlg0163
state = 59f45f0e9fcb6ebb
The snipit of what where the code runs.
get("/callback") {
val principal: OAuthAccessTokenResponse.OAuth2? = call.principal()
call.sessions.set(UserSession(principal!!.state!!, principal.accessToken)) //throws error
val redirect = redirects[principal.state!!]
call.respondRedirect(redirect!!)
}
I think you missed configuring your install(Sessions)
block, to use sessions, you have to pass the session id to the client, this can be done either using cookies or header:
install(Sessions) {
cookie<UserSession>("user_session", directorySessionStorage(File("build/.sessions"))) {
cookie.path = "/"
cookie.maxAgeInSeconds = 10
}
}
OR:
install(Sessions) {
header<UserSession>("user_session", directorySessionStorage(File("build/.sessions")))
}
Choose whatever you think is good for you. For more info, visit the documentation