forked from Scalingo/go-scalingo
/
login.go
57 lines (47 loc) · 1.22 KB
/
login.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
package scalingo
import (
"encoding/json"
"fmt"
"github.com/Scalingo/go-scalingo/http"
"gopkg.in/errgo.v1"
)
type LoginService interface {
Login(email, password string) (*LoginResponse, error)
}
var _ LoginService = (*Client)(nil)
type LoginError struct {
Success bool `json:"success"`
Message string `json:"message"`
}
type LoginResponse struct {
User *User `json:"user"`
}
func (err *LoginError) Error() string {
return err.Message
}
func (c *Client) Login(email, password string) (*LoginResponse, error) {
fmt.Println("[GO-SCALINGO] You are using the Login method. This method is deprecated, please use the OAuth flow")
req := &http.APIRequest{
NoAuth: true,
Method: "POST",
Endpoint: "/users/sign_in",
Expected: http.Statuses{201, 401},
Params: map[string]interface{}{
"user": map[string]string{
"login": email,
"password": password,
},
},
}
res, err := c.ScalingoAPI().Do(req)
if err != nil {
return nil, errgo.NoteMask(err, "fail to login", errgo.Any)
}
defer res.Body.Close()
var loginRes LoginResponse
err = json.NewDecoder(res.Body).Decode(&loginRes)
if err != nil {
return nil, errgo.NoteMask(err, "invalid response from server", errgo.Any)
}
return &loginRes, nil
}