-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser.go
More file actions
82 lines (73 loc) · 1.68 KB
/
Copy pathuser.go
File metadata and controls
82 lines (73 loc) · 1.68 KB
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
package leancloud
import (
"errors"
"net/url"
)
const userBaseURL = "users"
const userClass = "_User"
type User struct {
Object
}
func NewUser() *User {
o := NewObject()
return &User{*o}
}
func (u *User) Register(cloud *Client, username, password, email, phone string) (*result, error) {
u.Set("username", username)
u.Set("password", password)
u.Set("email", email)
u.Set("mobilePhoneNumber", phone)
url := cloud.makeURLPrefix(userBaseURL)
return cloud.httpPost(url, u.Encode())
}
func (u *User) Login(cloud *Client, username, password string) (*result, error) {
p := url.Values{}
p.Add("username", username)
p.Add("password", password)
uri := cloud.makeURLPrefix("login")
r, err := cloud.httpGet(uri, p)
if err != nil {
return r, err
}
o, err := r.Decode()
if err == nil {
u.Data = o.Data
}
return r, err
}
func (u *User) fetchFrom(cloud *Client, objectId string) (*result, error) {
url := cloud.makeURLPrefix(userBaseURL, objectId)
r, err := cloud.httpGet(url, nil)
if err != nil {
return r, err
}
u1, err := r.Decode()
if err == nil {
u.Data = u1.Data
}
return r, err
}
func FetchUser(cloud *Client, userId string) (*User, error) {
u := NewUser()
_, err := u.fetchFrom(cloud, userId)
return u, err
}
func GetUserSessionToken(cloud *Client, userId string) (string, error) {
r, err := CQLf(cloud, "select * from _User where objectId = '%s'", userId)
if err != nil {
return "", err
}
users, err := r.GetResults()
if err != nil {
return "", err
}
if len(users) == 0 {
return "", errors.New("empty results")
}
token, ok := users[0].Get("sessionToken").(string)
if ok {
return token, nil
} else {
return "", errors.New("convert to string failed")
}
}