sessions
sessions copied to clipboard
flash messages are not lost and are always saved when using gorilla mux
I use gorilla mux. When I access the flash message. The flash message is always stored and cannot be lost. Is there any solution ?
var SessionCookie = newCookieStore()
func newCookieStore() *sessions.CookieStore {
authKey := []byte("my-auth-key-very-secret")
encryptionKey := []byte("my-encryption-key-very-secret123")
store := sessions.NewCookieStore(authKey,encryptionKey)
store.Options.Path = "/"
store.Options.MaxAge = 86400 * 7 //expired dalam seminggu
store.Options.HttpOnly = true
return store
}
func SetFlashdata(w http.ResponseWriter, r *http.Request, name, value string){
session, _ := SessionCookie.Get(r, "fmessages")
session.AddFlash(value, name)
session.Save(r, w)
}
func GetFlashdata(w http.ResponseWriter, r *http.Request, name string) []string {
session, _ := SessionCookie.Get(r, "fmessages")
fm := session.Flashes(name)
//IF we have some message
if len(fm) > 0 {
session.Save(r, w)
//initiate a strings slice to return messages
var flashes []string
for _, fl := range fm {
//Add message to the slice
flashes = append(flashes, fl.(string))
}
return flashes
}
return nil
}
This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.
@madasatya6 i have a same problem. you found a solution?
@jackbaron https://github.com/gorilla/sessions/issues/163#issuecomment-411583840