Sunday, 6 February 2022

Ktor and Android - Session Time Limit

I've successfully set a Time-Limit for a Session on my Ktor server. Now the session no longer lives for 7 days (which is a default value), now it's life span is equals to: 'Session':

fun Application.configureSession() {
    install(Sessions) {
        val secretEncryptKey = hex("00112233445566778899aabbccddeeff")
        val secretAuthKey = hex("02030405060708090a0b0c")
        cookie<UserSession>(
            name = "USER_SESSION",
            storage = directorySessionStorage(File(".sessions"))
        ) {
            cookie.maxAgeInSeconds = 0
            transform(SessionTransportTransformerEncrypt(secretEncryptKey, secretAuthKey))
        }
    }
}

When Session life span is equals to 'Session', it means that the session should be automatically cleaned up immediately after the browser, for example is closed, on the client side. In this case I'm using Android app for a client, and whenever I close my app, I'm expecting to terminate/close that session and remove it from the server automatically. But sadly it's still available on my server, inside a .sessions folder. I'm able to remove it only after I explicitly call clear function from my server, to clear that session.

Is there any solution for me to make my Ktor server do a cleanup, and remove the session by default whenever I close my Android app and terminate the connection?



from Ktor and Android - Session Time Limit

No comments:

Post a Comment