Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
madasatya6 opened this issue Oct 16, 2021 · 3 comments
Open
Labels

Comments

@madasatya6
Copy link

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
Copy link

stale bot commented Jan 9, 2022

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 stale bot added the stale label Jan 9, 2022
@jackbaron
Copy link

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

@stale stale bot removed the stale label Mar 29, 2022
@coreydaley coreydaley added bug and removed question labels Jul 16, 2023
@oodzchen
Copy link

@jackbaron #163 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

4 participants