-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
79 lines (56 loc) · 1.73 KB
/
session.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
75
76
77
78
79
package tasty
import (
"net/http"
)
// Create a new user session.
func (c *Client) CreateSession(login LoginInfo, twoFactorCode *string) (Session, *http.Response, error) {
path := "/sessions"
type sessionResponse struct {
Session Session `json:"data"`
}
session := new(sessionResponse)
header := http.Header{}
if twoFactorCode != nil {
header.Add("X-Tastyworks-OTP", *twoFactorCode)
}
resp, err := c.noAuthRequest(http.MethodPost, path, header, nil, login, session)
if err != nil {
return Session{}, resp, err
}
c.Session = session.Session
return session.Session, resp, nil
}
// Validate the user session.
func (c *Client) ValidateSession() (User, *http.Response, error) {
path := "/sessions/validate"
type validSessionResponse struct {
User User `json:"data"`
}
user := new(validSessionResponse)
resp, err := c.request(http.MethodPost, path, nil, nil, user)
if err != nil {
return User{}, resp, err
}
c.Session.User = user.User
return user.User, resp, nil
}
// Destroy the user session and invalidate the token.
func (c *Client) DestroySession() (*http.Response, error) {
path := "/sessions"
return c.request(http.MethodDelete, path, nil, nil, nil)
}
// Request a password reset email.
func (c *Client) RequestPasswordResetEmail(email string) (*http.Response, error) {
path := "/password/reset"
type reset struct {
Email string `json:"email"`
}
resetInfo := new(reset)
resetInfo.Email = email
return c.noAuthRequest(http.MethodPost, path, http.Header{}, nil, resetInfo, nil)
}
// Request a password reset email.
func (c *Client) ChangePassword(resetInfo PasswordReset) (*http.Response, error) {
path := "/password"
return c.noAuthRequest(http.MethodPost, path, http.Header{}, nil, resetInfo, nil)
}