-
Notifications
You must be signed in to change notification settings - Fork 11
/
session.go
45 lines (39 loc) · 958 Bytes
/
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 readas
import (
"encoding/gob"
"github.com/gorilla/sessions"
"net/http"
"strings"
)
const (
day = 86400
sessionLength = 180 * day
)
// initSession creates the cookie store. It depends on the keychain already
// being loaded.
func initSession(app *app) error {
// Register complex data types we'll be storing in cookies
gob.Register(&LocalUser{})
// Create the cookie store
app.sStore = sessions.NewCookieStore(app.keys.cookieAuthKey, app.keys.cookieKey)
app.sStore.Options = &sessions.Options{
Path: "/",
MaxAge: sessionLength,
HttpOnly: true,
Secure: strings.HasPrefix(app.cfg.Host, "https://"),
}
return nil
}
func getUserSession(app *app, r *http.Request) *LocalUser {
session, err := app.sStore.Get(r, "u")
if err == nil {
// Got the currently logged-in user
val := session.Values["user"]
var u = &LocalUser{}
var ok bool
if u, ok = val.(*LocalUser); ok {
return u
}
}
return nil
}