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

Ranging over session key/value pairs #34

Open
mrichman opened this issue Aug 10, 2017 · 4 comments
Open

Ranging over session key/value pairs #34

mrichman opened this issue Aug 10, 2017 · 4 comments

Comments

@mrichman
Copy link

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

@SilverCory
Copy link
Contributor

A look at the clear function gives light on this!
#52

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

@TJM
Copy link
Contributor

TJM commented Feb 1, 2021

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

sessions/sessions.go

Lines 24 to 47 in 19716a2

type Session interface {
// ID of the session, generated by stores. It should not be used for user data.
ID() string
// Get returns the session value associated to the given key.
Get(key interface{}) interface{}
// Set sets the session value associated to the given key.
Set(key interface{}, val interface{})
// Delete removes the session value associated to the given key.
Delete(key interface{})
// Clear deletes all values in the session.
Clear()
// AddFlash adds a flash message to the session.
// A single variadic argument is accepted, and it is optional: it defines the flash key.
// If not defined "_flash" is used by default.
AddFlash(value interface{}, vars ...string)
// Flashes returns a slice of flash messages from the session.
// A single variadic argument is accepted, and it is optional: it defines the flash key.
// If not defined "_flash" is used by default.
Flashes(vars ...string) []interface{}
// Options sets configuration for a session.
Options(Options)
// Save saves all sessions used during the current request.
Save() error
}

@SilverCory
Copy link
Contributor

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

TJM commented Feb 1, 2021

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

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

3 participants