gin-contrib / sessions

Gin middleware for session management
MIT License
1.45k stars 194 forks source link

Ranging over session key/value pairs #34

Open mrichman opened 7 years ago

mrichman commented 7 years ago

Is it possible to range over the key/value pairs in session so I can log them?

SilverCory commented 6 years ago

A look at the clear function gives light on this! https://github.com/gin-contrib/sessions/pull/52

s.Session().Values appears to be a list of all keys!

TJM commented 3 years ago

my session does not have .Session()? I assume that is what you meant by "s" ? Maybe?

    r.GET("/", func(c *gin.Context) {
    session := sessions.Default(c)
// the rest

... session is an interface https://github.com/gin-contrib/sessions/blob/19716a227025fb465a37425826be04c5fef8eb98/sessions.go#L24-L47

SilverCory commented 3 years ago

@TJM I'm not sure that was 2 years ago, context could have been better on my behalf.

Reevaluating this, if you cast it to the actual implementation of the Session interface then there is an object in there.

For example the cookie implementation is: https://github.com/gorilla/sessions/blob/master/sessions.go#L31

TJM commented 3 years ago

Sorry, I realized I had responded to a zombie issue after I already hit submit :)

Thanks, I will try that "cast" idea. Displaying all keys in the session is really sortof a debugging operation anyhow, but I can see how it might be useful enough to add a session.GetValues() method. What do you think?

Also, the "example" code for session/cookie does not encrypt the cookies, so I was able to just use "Inspect" -> application -> cookie... and base64 decode (it was two different layers), and see what I was trying to see at the client level.

I was going to try to just "output" (c.JSON) everything in the session, but then I realized it was a waste of time/effort as that would not be something anyone would likely want to do.

Thanks, Tommy