-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
67 lines (58 loc) · 1.72 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
package yeahapi
import (
"context"
"github.com/gofrs/uuid"
)
type UserID struct {
uuid.UUID
}
const (
authProviderGoogle string = "google"
authProviderTelegram string = "telegram"
)
type User struct {
ID UserID `json:"id"`
PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Username string `json:"username"`
EmailVerified bool `json:"-"`
PhoneVerified bool `json:"-"`
}
type Account struct {
ID uuid.UUID
Provider string
UserID UserID
ProviderAccountID string
}
type UserService interface {
CreateUser(ctx context.Context, user *User) (*User, error)
ByPhone(ctx context.Context, phone string) (*User, error)
ByEmail(ctx context.Context, email string) (*User, error)
User(ctx context.Context, id UserID) (*User, error)
Account(ctx context.Context, id uuid.UUID) (*Account, error)
LinkAccount(ctx context.Context, account *Account) error
}
func (a *Account) Ok() error {
if a.Provider == "" {
return E(EInvalid, "Provider is required")
} else if a.Provider != authProviderGoogle && a.Provider != authProviderTelegram {
return E(EInvalid, "Unsupported auth provider")
} else if a.ProviderAccountID == "" {
return E(EInvalid, "Provider account id is required")
} else if a.UserID.IsNil() {
return E(EInvalid, "User id is required")
}
return nil
}
func (u *User) Ok() error {
if u.PhoneNumber == "" && u.Email == "" {
return E(EInvalid, "Either phone number or email is required")
} else if u.FirstName == "" {
return E(EInvalid, "First name is required")
} else if u.LastName == "" {
return E(EInvalid, "Last name is required")
}
return nil
}