/
user.go
74 lines (62 loc) · 2.16 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
package resolvers
import (
"context"
"time"
"github.com/dollarkillerx/graphql_template/internal/conf"
"github.com/dollarkillerx/graphql_template/internal/generated"
"github.com/dollarkillerx/graphql_template/internal/pkg/enum"
"github.com/dollarkillerx/graphql_template/internal/pkg/errs"
"github.com/dollarkillerx/graphql_template/internal/utils"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func (r *mutationResolver) LoginByPassword(ctx context.Context, input *generated.LoginByPassword) (*generated.AuthPayload, error) {
captchaOK := utils.CheckImgCaptcha(r.cache, input.CaptchaID, input.CaptchaCode)
if !captchaOK {
return nil, errs.CaptchaCode
}
account, err := r.Storage.GetUserByAccount(input.Account)
if err != nil {
return nil, errs.LoginFailed
}
if account.Password != utils.GetPassword(input.Password, conf.CONFIG.Salt) {
return nil, errs.LoginFailed
}
token, err := utils.JWT.CreateToken(enum.AuthJWT{
generated.UserInformation{
Account: account.Account,
Role: account.Role,
AccountID: account.ID,
AccountName: account.Name,
},
}, time.Now().Add(time.Hour*24*7).Unix())
if err != nil {
return nil, errs.SystemError(err)
}
return &generated.AuthPayload{
AccessTokenString: token,
UserID: account.ID,
}, nil
}
func (r *mutationResolver) Registry(ctx context.Context, input *generated.Registry) (*wrapperspb.BoolValue, error) {
captchaOK := utils.CheckImgCaptcha(r.cache, input.CaptchaID, input.CaptchaCode)
if !captchaOK {
return nil, errs.CaptchaCode
}
err := r.Storage.AccountRegistry(input.Account, input.Name, utils.GetPassword(input.Password, conf.CONFIG.Salt), generated.RoleGeneralUser)
if err != nil {
return nil, errs.SystemError(err)
}
return &wrapperspb.BoolValue{Value: true}, nil
}
func (r *queryResolver) User(ctx context.Context) (*generated.UserInformation, error) {
fromContext, err := utils.GetUserInformationFromContext(ctx)
if err != nil {
return nil, errs.PleaseSignIn
}
return &generated.UserInformation{
AccountID: fromContext.AccountID,
Role: fromContext.Role,
Account: fromContext.Account,
AccountName: fromContext.AccountName,
}, nil
}