-
Notifications
You must be signed in to change notification settings - Fork 5
Add session helper functions for value manipulation #45
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||||
| package session | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "errors" | ||||||||||
| "net/http" | ||||||||||
|
|
||||||||||
| "github.com/gorilla/sessions" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // ErrSessionNotFound is returned when the session is not found in the request context. | ||||||||||
| var ErrSessionNotFound = errors.New("session not found in request context") | ||||||||||
|
|
||||||||||
| // Get retrieves a value from the session by key. | ||||||||||
| // Returns the value and true if found, or nil and false if not found. | ||||||||||
| // Returns an error if the session is not in the request context. | ||||||||||
| func Get(r *http.Request, key string) (any, bool, error) { | ||||||||||
| session, ok := r.Context().Value(ctxKey).(*sessions.Session) | ||||||||||
| if !ok || session == nil { | ||||||||||
| return nil, false, ErrSessionNotFound | ||||||||||
| } | ||||||||||
|
|
||||||||||
| val, exists := session.Values[key] | ||||||||||
| return val, exists, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Set stores a value in the session. | ||||||||||
| // Returns an error if the session is not in the request context. | ||||||||||
| func Set(r *http.Request, w http.ResponseWriter, key string, value any) error { | ||||||||||
|
||||||||||
| session, ok := r.Context().Value(ctxKey).(*sessions.Session) | ||||||||||
| if !ok || session == nil { | ||||||||||
| return ErrSessionNotFound | ||||||||||
| } | ||||||||||
|
|
||||||||||
| session.Values[key] = value | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Delete removes a value from the session by key. | ||||||||||
| // Returns an error if the session is not in the request context. | ||||||||||
|
Comment on lines
+38
to
+39
|
||||||||||
| func Delete(r *http.Request, w http.ResponseWriter, key string) error { | ||||||||||
|
||||||||||
| session, ok := r.Context().Value(ctxKey).(*sessions.Session) | ||||||||||
| if !ok || session == nil { | ||||||||||
| return ErrSessionNotFound | ||||||||||
| } | ||||||||||
|
|
||||||||||
| delete(session.Values, key) | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Clear removes all values from the session. | ||||||||||
| // Returns an error if the session is not in the request context. | ||||||||||
|
Comment on lines
+50
to
+51
|
||||||||||
| func Clear(r *http.Request, w http.ResponseWriter) error { | ||||||||||
|
||||||||||
| session, ok := r.Context().Value(ctxKey).(*sessions.Session) | ||||||||||
| if !ok || session == nil { | ||||||||||
| return ErrSessionNotFound | ||||||||||
| } | ||||||||||
|
|
||||||||||
| for k := range session.Values { | ||||||||||
| delete(session.Values, k) | ||||||||||
| } | ||||||||||
|
Comment on lines
+58
to
+60
|
||||||||||
| for k := range session.Values { | |
| delete(session.Values, k) | |
| } | |
| session.Values = make(map[interface{}]interface{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation should clarify that sessions are automatically saved when the response is written via the saver middleware, and that this function only modifies the session in memory. Users might expect that calling
Setimmediately persists the session, but it doesn't - it only modifies the session object which is later saved automatically.