-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
112 lines (100 loc) · 2.5 KB
/
user.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package handler
import (
"context"
"github.com/jinzhu/gorm"
"github.com/ghosv/open/meta"
"github.com/ghosv/open/plat/services/core/initialize"
model "github.com/ghosv/open/plat/services/core/model"
pb "github.com/ghosv/open/plat/services/core/proto"
notifyPb "github.com/ghosv/open/plat/services/notify/proto"
"github.com/ghosv/open/plat/utils"
)
// User Handler
type User struct{}
// Login Action
func (h *User) Login(ctx context.Context, req *pb.Credential, res *pb.Token) error {
name, phone, email, ok := utils.CheckUsername(req.NamePhoneEmail)
if !ok {
return meta.ErrWrongUsername
}
repo := ctx.Value(meta.KeyRepo).(*gorm.DB)
u := model.User{
Name: name,
Phone: phone,
Email: email,
Pass: req.Pass,
}
if result := repo.Where(&u).First(&u); result.Error != nil {
return meta.ErrCredential
}
token := NewToken()
token.Payload = u.TokenPayload()
var e error
if res.Str, e = token.Sign(initialize.DefaultJWTTerm); e != nil {
return e
}
return nil
}
// Register Action
func (h *User) Register(ctx context.Context, req *pb.Identity, res *pb.Token) error {
name, _, _, ok := utils.CheckUsername(req.Name)
if !(ok && name == req.Name) ||
!utils.IsPhone(req.Phone) ||
!utils.IsEmail(req.Email) {
return meta.ErrWrongUsername
}
// verify phone & email
c := ctx.Value(meta.KeyClientNotifyVerify).(notifyPb.VerifyService)
res0, err := c.CodeMatch(ctx, ¬ifyPb.VerifyCodeGroup{
Codes: []*notifyPb.VerifyCode{
¬ifyPb.VerifyCode{
Type: notifyPb.PostType_CodePhone,
To: req.Phone,
Code: req.PhoneCode,
},
¬ifyPb.VerifyCode{
Type: notifyPb.PostType_CodeEmail,
To: req.Email,
Code: req.EmailCode,
},
},
})
if err != nil {
return meta.ErrInvalidCode
}
for _, v := range res0.Codes {
if !v.Match {
return meta.ErrInvalidCode
}
}
repo := ctx.Value(meta.KeyRepo).(*gorm.DB)
u := &model.User{
Name: req.Name,
Phone: req.Phone,
Email: req.Email,
Pass: req.Pass,
Nick: req.Name,
}
if result := repo.Create(u); result.Error != nil {
return meta.ErrUserHasExist
}
token := NewToken()
token.Payload = u.TokenPayload()
var e error
if res.Str, e = token.Sign(initialize.DefaultJWTTerm); e != nil {
return e
}
return nil
}
// Check Token
func (h *User) Check(ctx context.Context, req *pb.Token, res *pb.Token) error {
token, err := ValidToken(req.Str)
if err != nil {
return err
}
t, err := FreshToken(token,
initialize.DefaultJWTFresh, initialize.DefaultJWTTerm)
res.Str = t
res.Payload = token.Payload
return nil
}