-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
58 lines (48 loc) · 1.14 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
package user
import (
"context"
"github.com/fox-one/mixin-sdk-go"
"github.com/fox-one/pando/core"
)
type Config struct {
ClientSecret string
}
func New(client *mixin.Client, cfg Config) core.UserService {
return &userService{
client: client,
secret: cfg.ClientSecret,
}
}
type userService struct {
client *mixin.Client
secret string
}
func (s *userService) Find(ctx context.Context, mixinID string) (*core.User, error) {
profile, err := s.client.ReadUser(ctx, mixinID)
if err != nil {
return nil, err
}
user := &core.User{
MixinID: profile.UserID,
Name: profile.FullName,
Avatar: profile.AvatarURL,
}
return user, nil
}
func (s *userService) Login(ctx context.Context, token string) (*core.User, error) {
profile, err := mixin.UserMe(ctx, token)
if err != nil {
return nil, err
}
user := &core.User{
MixinID: profile.UserID,
Name: profile.FullName,
Avatar: profile.AvatarURL,
AccessToken: token,
}
return user, nil
}
func (s *userService) Auth(ctx context.Context, code string) (string, error) {
token, _, err := mixin.AuthorizeToken(ctx, s.client.ClientID, s.secret, code, "")
return token, err
}