0

I want to set a cookie on my website using call.response.cookies.append("token", "$token"). How can I set up an expiration date for this cookie.

Anandakrishnan
  • 347
  • 1
  • 4
  • 18

1 Answers1

0

You can do something like the following:

val myCookie = Cookie(
        name = token,
        value = "$token",
        maxAge = 10
    )
call.response.cookies.append(myCookie)

The cookie data class is as follows from the documentation:

data class Cookie(
    val name: String, 
    val value: String, 
    val encoding: CookieEncoding = CookieEncoding.URI_ENCODING, 
    val maxAge: Int = 0, 
    val expires: GMTDate? = null, 
    val domain: String? = null, 
    val path: String? = null, 
    val secure: Boolean = false, 
    val httpOnly: Boolean = false, 
    val extensions: Map<String, String?> = emptyMap()
)

So for giving expiration time for a cookie, you can use either maxAge (which expects the time in seconds) or expiry (which expects a GMTDate). If both are set then maxAge takes priority (Refer this Q & A)

Additionally from the documentation itself:

A cookie with neither expires nor maxAge is a session cookie.

Anandakrishnan
  • 347
  • 1
  • 4
  • 18