I am using the following code to pass the current session throughout my package. I am building with fresh which watches my files. It seems after build the cookies are no longer valid? I've tried both a cookie store and a mysql store. I have confirmed the cookies are still in the browser and the line items are still in the db.
var sessionStore = sessions.NewCookieStore([]byte(os.Getenv("SESSION_SECRET")))
var sessionPointer *sessions.Session;
func initSession(r *http.Request) *sessions.Session {
if sessionPointer == nil {
} else{
return sessionPointer;
}
temp, err := sessionStore.Get(r,os.Getenv("SESSION_NAME"))
sessionPointer = temp;
sessionPointer.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 1,
HttpOnly: false,
}
if err != nil {
panic(err)
}
return sessionPointer
}
The issue is most likely that you are "sharing" sessions. A session should belong to one request. But when you do:
var sessionPointer *sessions.Session;
func initSession(r *http.Request) *sessions.Session {
if sessionPointer == nil {
} else{
return sessionPointer;
}
temp, err := sessionStore.Get(r,os.Getenv("SESSION_NAME"))
sessionPointer = temp;
// ...
}
You are essentially saying "if any session has been created, return it". This means that every request (and every user) will be getting the same session. I'm not sure how they go about matching a request to a specific session, but I'd imagine that not calling sessionStore.Get
each time you match it to a new request, eventually something gets set on the session that makes it no longer valid (because the session did not actually belong to that request).
The good news is that there is no reason to cache the sessionPointer. It's using a map internally anyways, and the lookup is essentially free. I would change your code to:
func initSession(r *http.Request) *sessions.Session {
temp, err := sessionStore.Get(r,os.Getenv("SESSION_NAME"))
if err != nil {
panic(err)
}
sessionPointer.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 1,
HttpOnly: false,
}
return sessionPointer
}
and then I would think that everything would work as expected.