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

session flashes not clearing #54

Open
clanstyles opened this issue Jul 22, 2015 · 10 comments
Open

session flashes not clearing #54

clanstyles opened this issue Jul 22, 2015 · 10 comments

Comments

@clanstyles
Copy link

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.

@lauborges
Copy link

+1

1 similar comment
@vodolaz095
Copy link

+1

@vodolaz095
Copy link

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,
        })
    })

@mrichman
Copy link

mrichman commented Aug 7, 2017

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?

@vodolaz095
Copy link

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

@mrichman
Copy link

mrichman commented Aug 7, 2017

@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
	}

@tleb
Copy link

tleb commented Aug 7, 2017

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

@mrichman
Copy link

mrichman commented Aug 8, 2017

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

@tleb
Copy link

tleb commented Aug 8, 2017

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.

@mrichman
Copy link

mrichman commented Aug 8, 2017

@clanstyles Does this solve your original problem as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants