forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
46 lines (38 loc) · 1.08 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package buffalo
import (
"net/http"
"github.com/gorilla/sessions"
)
// Session wraps the "github.com/gorilla/sessions" API
// in something a little cleaner and a bit more useable.
type Session struct {
Session *sessions.Session
req *http.Request
res http.ResponseWriter
}
// Save the current session.
func (s *Session) Save() error {
return s.Session.Save(s.req, s.res)
}
// Get a value from the current session.
func (s *Session) Get(name interface{}) interface{} {
return s.Session.Values[name]
}
// Set a value onto the current session. If a value with that name
// already exists it will be overridden with the new value.
func (s *Session) Set(name, value interface{}) {
s.Session.Values[name] = value
}
// Delete a value from the current session.
func (s *Session) Delete(name interface{}) {
delete(s.Session.Values, name)
}
// Get a session using a request and response.
func (a *App) getSession(r *http.Request, w http.ResponseWriter) *Session {
session, _ := a.SessionStore.Get(r, a.SessionName)
return &Session{
Session: session,
req: r,
res: w,
}
}