-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
46 lines (38 loc) · 965 Bytes
/
auth.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
package hutplate
import (
"errors"
"golang.org/x/crypto/bcrypt"
)
type Auth struct {
session session
}
func (auth Auth) Login(email string, password string) (bool, error) {
userId, userPassword := Config.GetUserWithCred(email)
if bcrypt.CompareHashAndPassword([]byte(userPassword), []byte(password)) == nil {
if err := auth.session.Set("user_id", userId); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
func (auth Auth) Check() bool {
return auth.UserId() != nil
}
func (auth Auth) UserId() interface{} {
userId, _ := auth.session.Get("user_id")
return userId
}
func (auth Auth) User() (interface{}, error) {
if Config.GetUserWithId == nil {
return nil, errors.New("Config GetUserWithId is not defined")
}
userId, err := auth.session.Get("user_id")
if err != nil {
return nil, err
}
return Config.GetUserWithId(userId), nil
}
func (auth Auth) Logout() error {
return auth.session.Set("user_id", nil)
}