Agreensh
Agreensh

Reputation: 1401

Set the user session expiry

I need to set the expiry time for the user session in Vapor 2 (or for cookies in general), but can't see how to do it. Can anyone help?

Thanks, Andy

Upvotes: 2

Views: 608

Answers (1)

tobygriffin
tobygriffin

Reputation: 5421

Cookies can be set with a max-age after which they expire. This is the best way to set a time limit on your user's sessions.

Unfortunately Vapor's SessionsMiddleware doesn't include baked-in support for putting an age limit on the session cookie, but it's not too hard to create the SessionsMiddleware manually and override the cookieFactory closure.

let sessionsMiddleware = try SessionsMiddleware(config.resolveSessions(), cookieName: "my-sessions-cookie-name", cookieFactory: { request in
  return Cookie(
    name: "my-sessions-cookie-name",
    value: "",
    maxAge: 3600,
    httpOnly: true
  )
})

Upvotes: 2

Related Questions