sessions icon indicating copy to clipboard operation
sessions copied to clipboard

Ranging over session key/value pairs

Open mrichman opened this issue 6 years ago • 4 comments

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

mrichman avatar Aug 10 '17 17:08 mrichman

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!

SilverCory avatar Jun 03 '18 23:06 SilverCory

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

TJM avatar Feb 01 '21 01:02 TJM

@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

SilverCory avatar Feb 01 '21 19:02 SilverCory

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

TJM avatar Feb 01 '21 19:02 TJM