-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_manager.go
56 lines (50 loc) · 1.24 KB
/
session_manager.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
47
48
49
50
51
52
53
54
55
56
package session
import (
"github.com/gorilla/sessions"
"github.com/daniloanp/Ensaios/application/backend/model"
"net/http"
"encoding/gob"
"github.com/daniloanp/Ensaios/application/backend/app"
)
var store = sessions.NewCookieStore([]byte("something-very-secret"))
const (
dataKey = string("data")
sessionName = "Session_Name"
sessionErrorMessage = "Couldn't create session!!!"
)
type Session struct {
*sessions.Session
User *model.UserAccount // maybe not
Role *model.RoleData // maybe not
}
func GetSessionData(w http.ResponseWriter, r *http.Request) *Session {
var mySession *Session
session, err := store.Get(r, sessionName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) //TODO:RESOLVE_IT
return nil
}
if session.IsNew {
_, role := app.Db().Role.GetByID(app.AnonymousRole); //TODO:Ignoring Error
mySession = &Session{
Session: session,
User: nil,
Role: role ,
}
session.Values[dataKey] = mySession
session.Save(r,w)
} else {
var (
ok bool
value = session.Values[dataKey]
)
if mySession, ok = value.(*Session); !ok {
http.Error(w, err.Error(), http.StatusInternalServerError) //TODO:RESOLVE_IT
return nil
}
}
return mySession
}
func init() {
gob.Register(&Session{})
}