sessions
sessions copied to clipboard
Call Save() in the middleware?
As far as I can tell, the sessions middleware that is provided in this package, aka gin.HandlerFunc
doesn't call the Save()
method after the rest of the middleware stack has ran, I can't think of a real reason to not save at this point (I've resorted to creating my own middleware that I insert into the stack after the sessions one to automatically call save.
This is currently the middleware:
func Sessions(name string, store Store) gin.HandlerFunc {
return func(c *gin.Context) {
s := &session{name, c.Request, store, nil, false, c.Writer}
c.Set(DefaultKey, s)
defer context.Clear(c.Request)
c.Next()
}
}
In my eyes, after the c.Next()
we'd call s.Save()
to actually persist any changes to the session. Thoughts?
@bobbytables Haven't tested, but I believe Set-Cookie
header won't be sent AFTER any response is written?
GoDoc of http.ResponseWriter says that any modification to the header after the first call of Write()
will have no effect unless it's formerly declared as trailers header.
So, calling s.Save()
after c.Next()
is too late, since the main handler obviously writes the response (which internally calls ResponseWriter.Write()
)