sessions
sessions copied to clipboard
Is the cookie stored in file system?
When I restart server, I found that old cookie was still work. Is the cookie stored in file system? If so, where the cookie was stored (file path)
I was equally puzzled and wished to know the answer
gsessions "github.com/gorilla/sessions"
I'm in the source code, and it's based on the file system
I don't think FileSystemStore
is used in this gin-contrib/sessions
, so I assume you are using the cookie-based store.
Here is the Save
function for cookie-based store. You can see the session.Values
is encoded and saved into the cookie returned to the browser. So technically the session is saved in the client browser :D
// Save adds a single session to the response.
func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter,
session *Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.Codecs...)
if err != nil {
return err
}
http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
return nil
}
So as long as you use the same secret for the store and the cookie is decode-able, the session will still work. It took me a while to read through the source code and figure it out :D. Hope this will help.
I don't think
FileSystemStore
is used in thisgin-contrib/sessions
, so I assume you are using the cookie-based store.Here is the
Save
function for cookie-based store. You can see thesession.Values
is encoded and saved into the cookie returned to the browser. So technically the session is saved in the client browser :D// Save adds a single session to the response. func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, session *Session) error { encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, s.Codecs...) if err != nil { return err } http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options)) return nil }
So as long as you use the same secret for the store and the cookie is decode-able, the session will still work. It took me a while to read through the source code and figure it out :D. Hope this will help.
Thanks!