sessions icon indicating copy to clipboard operation
sessions copied to clipboard

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

Open madasatya6 opened this issue 4 years ago • 3 comments

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
}

madasatya6 avatar Oct 16 '21 12:10 madasatya6

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.

stale[bot] avatar Jan 09 '22 03:01 stale[bot]

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

jackbaron avatar Mar 29 '22 03:03 jackbaron

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

oodzchen avatar Jul 31 '23 14:07 oodzchen