-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
68 lines (60 loc) · 1.46 KB
/
Copy pathauth.go
File metadata and controls
68 lines (60 loc) · 1.46 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
package discord
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
//Request a token from web server
func (c *Client) authLogin(email string, pass string) (err error) {
httpClient := &http.Client{Timeout: (20 * time.Second)}
var resp *http.Response
resp, err = httpClient.Post(DISCORD_URL+"auth/login", "application/json", bytes.NewBuffer([]byte(fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, pass))))
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("StatusCode: %d, %s", resp.StatusCode, string(body))
return
}
err = json.Unmarshal(body, &c)
if err != nil {
return
}
return
}
func (c *Client) authLogout() (err error) {
if !c.IsLoggedIn() {
err = errors.New("You must be logged in")
return
}
httpClient := &http.Client{Timeout: (20 * time.Second)}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", DISCORD_URL, fmt.Sprintf("auth/logout")), bytes.NewBuffer([]byte(fmt.Sprintf(``))))
if err != nil {
return
}
req.Header.Set("authorization", c.Token)
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
resp.Body.Close()
if resp.StatusCode != 204 && resp.StatusCode != 200 {
err = fmt.Errorf("StatusCode: %d, %s", resp.StatusCode, string(body))
return
}
return
}