I've read all the related questions regarding this and still can't manage to get it working.
I am using Chi router and Gorilla Sessions.
First I init the store.Options
:
var authKey = []byte("somesecret")
// Encryption Key
var encKey = []byte("someothersecrets")
var store = sessions.NewCookieStore(authKey, encKey)
func init() {
fmt.Println("$$$%%$$%%%%%%")
store.Options = &sessions.Options{
Domain: "localhost",
Path: "/",
MaxAge: 3600 * 8, // 8 hours
HttpOnly: true,
}
Then when the first route handler is hit, I generate a new state
variable and save it to the session:
session, err = store.Get(r, "my_cookie")
session.Values["state"] = newState
errSave := session.Save(r, w)
This saves without error. Next the client does some stuff and hits a different route handler where I try to grab that state
variable out of the session:
session, err = store.Get(r, "my_cookie")
existingState := session.Values["state"]
However this existing state value is always nil, the session.IsNew
always returns true here as well.
I've managed to solve it by making session
a global defined variable and not re-calling
session, err = store.Get(r, "my_cookie")
in the second handler but this seems like the wrong approach. It seems like since I am calling store.get()
with a different request object each time that it is creating a new store but this seems completely contradictory to the purpose of this library. Can anyone shed light on what I'm missing here?