Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gorilla Sessions - How to Automatically Update Cookie Expiration on Request?

I know many other languages and web frameworks will automatically update a cookie's expiration time to the session timeout every time a session is accessed via the backend (or some action like that). I don't believe Gorilla provides this utility.

I am consider just writing some request middleware that, if it detects a valid session, will extend the cookie lifetime but I am wondering if there is a better method of doing this.

What are the best practices for updating cookie expiration, especially as they pertain to Gorilla/Go?

like image 223
Darrrrrren Avatar asked Nov 01 '22 09:11

Darrrrrren


2 Answers

You could simply implement your own Store that builds on top of an existing session store like the CookieStore, but uses some rule to automatically update the expiration during a Save call.

like image 60
Emil Davtyan Avatar answered Nov 09 '22 08:11

Emil Davtyan


If you set the Max-Age parameter of the cookie, you don't need to set Expiry as well, unless you need to support old browsers that don't understand Max-Age.

Using only Max-Age means you don't need to update it on every request.

Set the spec: https://www.rfc-editor.org/rfc/rfc6265#section-4.1.2

like image 26
Rick-777 Avatar answered Nov 09 '22 07:11

Rick-777