-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathstorer.go
71 lines (59 loc) · 1.74 KB
/
storer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package server
import (
"context"
"net/url"
"github.com/jinzhu/gorm"
abclientstate "github.com/volatiletech/authboss-clientstate"
"github.com/volatiletech/authboss/v3"
)
/////////////////////////////////////////////////////////////////
// ServerStorer
type SQLiteStorer struct {
authboss.CreatingServerStorer
DB *gorm.DB
}
func (s SQLiteStorer) Load(ctx context.Context, key string) (authboss.User, error) {
user := User{
ID: url.QueryEscape(key), // Encode the email to userID
}
if err := s.DB.First(&user).Error; err != nil {
return &user, authboss.ErrUserNotFound
}
return &user, nil
}
func (s SQLiteStorer) Save(ctx context.Context, user authboss.User) error {
user = user.(*User)
err := s.DB.Save(&user).Error
return err
}
func (s SQLiteStorer) New(ctx context.Context) authboss.User {
return &User{
ListMode: false,
DarkMode: false,
}
}
func (s SQLiteStorer) Create(ctx context.Context, user authboss.User) error {
existingUser := user.(*User)
existingUser.ID = url.QueryEscape(existingUser.ID)
if err := s.DB.First(&existingUser).Error; err == nil {
return authboss.ErrUserFound
}
err := s.DB.Create(&existingUser).Error
return err
}
////////////////////////////////////////////////////////////
// Factory Methods
func NewSQLiteStorer(db *gorm.DB) *SQLiteStorer {
return &SQLiteStorer{
DB: db,
}
}
func NewCookieStorer(cookieStoreKey []byte, isSecure bool) abclientstate.CookieStorer {
newCookieStore := abclientstate.NewCookieStorer(cookieStoreKey, nil)
newCookieStore.HTTPOnly = isSecure
newCookieStore.Secure = isSecure
return newCookieStore
}
func NewSessionStorer(cookieName string, sessionStoreKey []byte) abclientstate.SessionStorer {
return abclientstate.NewSessionStorer(cookieName, sessionStoreKey, nil)
}