-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
45 lines (33 loc) · 1.18 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
package store
import (
"encoding/base64"
"net/http"
"github.com/gorilla/sessions"
)
type SessionStore interface {
Get(r *http.Request, name string) (*sessions.Session, error)
}
func GetSession(r *http.Request, cookieName string, sessionStore SessionStore) (*sessions.Session, error) {
return sessionStore.Get(r, cookieName)
}
func SetSessionB64(r *http.Request, w http.ResponseWriter, body []byte, cookieName, valueName string, sessionStore SessionStore) (string, error) {
cookieValue := base64.StdEncoding.EncodeToString(body)
if err := SetSession(r, w, cookieValue, cookieName, valueName, sessionStore); err != nil {
return "", err
}
return cookieValue, nil
}
func SetSession(r *http.Request, w http.ResponseWriter, value, cookieName, valueName string, sessionStore SessionStore) error {
// set the cookie
session, _ := sessionStore.Get(r, cookieName)
session.Values[valueName] = value
if err := session.Save(r, w); err != nil {
return err
}
return nil
}
func RemoveSession(r *http.Request, w http.ResponseWriter, cookieName string, sessionStore SessionStore) error {
session, _ := sessionStore.Get(r, cookieName)
session.Options.MaxAge = -1
return session.Save(r, w)
}