Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ktor and Kvision Sessions #25

Closed
robert-cronin opened this issue Mar 27, 2019 · 2 comments
Closed

Ktor and Kvision Sessions #25

robert-cronin opened this issue Mar 27, 2019 · 2 comments

Comments

@robert-cronin
Copy link
Contributor

In the Kvision-Ktor framework, I can define a service manager and automate routing like so:

applyRoutes(AuthenticationServiceManager)

However, I would also like to use Ktor sessions and cookies for tracking users. With Ktor routing I can do the following:

data class SampleSession(val name: String, val value: Int)
...
get("/get") {
    val session = call.sessions.get<SampleSession>() ?: SampleSession(0) 
    call.respondHtml {"Counter: ${session.counter}"}
}

What's the best way to achieve this using ktor but access the result as a function from Kvision?

@rjaros
Copy link
Owner

rjaros commented Mar 27, 2019

Use dependency injection and inject ApplicationCall instance into your Service class. Then you can use the same code as above. Here is the example of injection: https://github.com/rjaros/kvision-examples/blob/17acf84f31923ec2c40d3d75696fdfa6db941d0b/addressbook-fullstack-ktor/server/src/main/kotlin/com/example/Service.kt#L22

@robert-cronin
Copy link
Contributor Author

Closing this as problem solved 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants