-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
47 lines (38 loc) · 959 Bytes
/
sessions.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
package web
import (
"context"
"database/sql"
"encoding/gob"
"github.com/alexedwards/scs/postgresstore"
"github.com/alexedwards/scs/v2"
"github.com/google/uuid"
"github.com/noloman/goreddit"
)
func init() {
gob.Register(uuid.UUID{})
}
func NewSessionManager(dataSourceName string) (*scs.SessionManager, error) {
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
sessions := scs.New()
sessions.Store = postgresstore.New(db)
return sessions, nil
}
type SessionData struct {
FlashMessage string
Form interface{}
User goreddit.User
LoggedIn bool
}
func GetSessionData(session *scs.SessionManager, ctx context.Context) SessionData {
var data SessionData
data.FlashMessage = session.PopString(ctx, "flash")
data.User, data.LoggedIn = ctx.Value("user").(goreddit.User)
data.Form = session.Pop(ctx, "form")
if data.Form == nil {
data.Form = map[string]string{}
}
return data
}