gorilla / sessions

Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
2.93k stars 371 forks source link

flash messages are not lost and are always saved when using gorilla mux #243

Closed madasatya6 closed 5 months ago

madasatya6 commented 3 years ago

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
}
stale[bot] commented 2 years ago

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.

jackbaron commented 2 years ago

@madasatya6 i have a same problem. you found a solution?

oodzchen commented 1 year ago

@jackbaron https://github.com/gorilla/sessions/issues/163#issuecomment-411583840

jaitaiwan commented 5 months ago

Closing out as per the referenced comment. Thanks!