contrib icon indicating copy to clipboard operation
contrib copied to clipboard

session flashes not clearing

Open peternavr opened this issue 10 years ago • 10 comments

Session flashes aren't clearing I'm using go version go1.4.2 gccgo (GCC) 5.1.0 linux/amd64

There is only one "add flash" called and one "save" and then one "get flashes" and in Gorilla they're clearing, but they're not actually clearing on the next call. It removes them from the linked list but then a copy of gin somewhere must be taking over.

peternavr avatar Jul 22 '15 22:07 peternavr

+1

lauborges avatar Aug 31 '15 20:08 lauborges

+1

vodolaz095 avatar May 02 '16 18:05 vodolaz095

for me it worked by calling saving session

    v1.GET("/", func(c *gin.Context) {
        session := sessions.Default(c)
        flashes := session.Flashes() 
        session.Save() // <---- here it is!
        c.HTML(http.StatusOK, "index.html", gin.H{
            "flash":  flashes,
        })
    })

vodolaz095 avatar May 02 '16 18:05 vodolaz095

I'm doing the same thing, and I see the flashes rendered by my template like this:

{{ range $f := .flash }}
<div class="alert alert-danger">{{ $f }}</div>
{{ end }}

However, if I see the flash message, then navigate to another page, the flash message persists until I navigate again.

What am I doing wrong?

mrichman avatar Aug 07 '17 16:08 mrichman

maybe call session.Save() // <---- here it is!? to save session with empty flashes

vodolaz095 avatar Aug 07 '17 17:08 vodolaz095

@vodolaz095 I'm already doing that quite liberally:

if u, _ := findUserByUsername(user.Username); u != nil {
		msg := fmt.Sprintf("User already exists: %s", user.Username)
		log.Println(msg)
		session.AddFlash(msg)
		err := session.Save()  // <---- see, right there :) 

		if err != nil {
			log.Println(err)
		}

		c.HTML(http.StatusOK, "signup.html", gin.H{
			"flash": session.Flashes(),
			"user":  user,
		})
		return
	}

mrichman avatar Aug 07 '17 17:08 mrichman

Have you tried saving just before the return (therefore after session.Flashes())?

tleb avatar Aug 07 '17 21:08 tleb

This seems to work, though I'm not sure why.

mrichman avatar Aug 08 '17 13:08 mrichman

The principle of flashes is that you read them once then delete them. It's what sessions.Flashes() does but you need to save to remember the deletion.

tleb avatar Aug 08 '17 13:08 tleb

@clanstyles Does this solve your original problem as well?

mrichman avatar Aug 08 '17 14:08 mrichman