/
auth.go
61 lines (52 loc) · 1.28 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package biz
import (
"context"
"github.com/golang-jwt/jwt"
"github.com/lalifeier/vvgo-mall/app/shop/admin/internal/conf"
)
type AccountUser struct {
Id int64
Username string
Password string
Email string
Phone string
Token string
}
type AccountRepo interface {
Register(ctx context.Context, u *AccountUser) (int64, error)
Login(ctx context.Context, u *AccountUser) (*AccountUser, error)
}
type AuthUseCase struct {
key string
userRepo UserRepo
accountRepo AccountRepo
}
func NewAuthUseCase(conf *conf.Auth, userRepo UserRepo, accountRepo AccountRepo) *AuthUseCase {
return &AuthUseCase{
key: conf.ApiKey,
userRepo: userRepo,
accountRepo: accountRepo,
}
}
func (uc *AuthUseCase) Register(ctx context.Context, u *AccountUser) (int64, error) {
id, err := uc.accountRepo.Register(ctx, u)
if err != nil {
return 0, err
}
return id, nil
}
func (uc *AuthUseCase) Login(ctx context.Context, u *AccountUser) (*AccountUser, error) {
user, err := uc.accountRepo.Login(ctx, u)
if err != nil {
return nil, err
}
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": user.Id,
})
signedString, err := claims.SignedString([]byte(uc.key))
if err != nil {
return nil, err
}
user.Token = signedString
return user, nil
}