forked from goffee/goffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
34 lines (28 loc) · 846 Bytes
/
helpers.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
package helpers
import (
"errors"
"github.com/gophergala/goffee/Godeps/_workspace/src/github.com/gorilla/sessions"
"github.com/gophergala/goffee/Godeps/_workspace/src/github.com/zenazn/goji/web"
"github.com/gophergala/goffee/data"
)
// CurrentSession returns the current session
func CurrentSession(c web.C) *sessions.Session {
session := c.Env["Session"].(*sessions.Session)
return session
}
// CurrentUser returns the current user
func CurrentUser(c web.C) (data.User, error) {
session := CurrentSession(c)
userID := session.Values["UserId"]
switch userID := userID.(type) {
case int64:
return data.FindUser(userID)
default:
return data.User{}, errors.New("User not found")
}
}
// UserSignedIn returns true if there is an authenticated user
func UserSignedIn(c web.C) bool {
_, err := CurrentUser(c)
return err == nil
}