Files
tinder-api-wrapper/auth.go

128 lines
3.6 KiB
Go

package tinder
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// AuthRequest represents the request body for authentication
type AuthRequest struct {
PhoneNumber string `json:"phone_number,omitempty"`
Email string `json:"email,omitempty"`
FacebookID string `json:"facebook_id,omitempty"`
FacebookToken string `json:"facebook_token,omitempty"`
OTP string `json:"otp,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
}
// AuthResponse represents the response from authentication endpoints
type AuthResponse struct {
Meta struct {
Status int `json:"status"`
Message string `json:"message,omitempty"`
} `json:"meta"`
Data struct {
APIToken string `json:"api_token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
IsNewUser bool `json:"is_new_user,omitempty"`
SMSSent bool `json:"sms_sent,omitempty"`
} `json:"data"`
}
// ProfileResponse represents the response from the profile endpoint
type ProfileResponse struct {
Meta struct {
Status int `json:"status"`
} `json:"meta"`
Data struct {
User struct {
ID string `json:"_id"`
Bio string `json:"bio"`
Name string `json:"name"`
} `json:"user"`
} `json:"data"`
}
// SendPhoneLogin initiates a phone login by requesting an OTP code
func (c *Client) SendPhoneLogin(phoneNumber string) (*AuthResponse, error) {
authReq := AuthRequest{
PhoneNumber: phoneNumber,
}
return c.doAuthRequest("/v2/auth/sms/send", authReq)
}
// LoginWithOTP completes the phone login using the provided OTP code
func (c *Client) LoginWithOTP(phoneNumber, otp string) (*AuthResponse, error) {
authReq := AuthRequest{
PhoneNumber: phoneNumber,
OTP: otp,
}
return c.doAuthRequest("/v2/auth/sms/validate", authReq)
}
// LoginWithFacebook authenticates using a Facebook ID and token
func (c *Client) LoginWithFacebook(facebookID, facebookToken string) (*AuthResponse, error) {
authReq := AuthRequest{
FacebookID: facebookID,
FacebookToken: facebookToken,
}
return c.doAuthRequest("/v2/auth/facebook", authReq)
}
// RefreshAuth refreshes the authentication token
func (c *Client) RefreshAuth(refreshToken string) (*AuthResponse, error) {
authReq := AuthRequest{
RefreshToken: refreshToken,
}
return c.doAuthRequest("/v2/auth/login/refresh", authReq)
}
// GetProfile retrieves the current user's profile
func (c *Client) GetProfile() (*ProfileResponse, error) {
resp, err := c.doRequest(http.MethodGet, "/v2/profile", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %s (status code: %d)", body, resp.StatusCode)
}
var profileResp ProfileResponse
if err := json.NewDecoder(resp.Body).Decode(&profileResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &profileResp, nil
}
// doAuthRequest performs an authentication request and handles common auth response processing
func (c *Client) doAuthRequest(path string, authReq AuthRequest) (*AuthResponse, error) {
resp, err := c.doRequest(http.MethodPost, path, authReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %s (status code: %d)", body, resp.StatusCode)
}
var authResp AuthResponse
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
// Set the auth token if it was returned
if authResp.Data.APIToken != "" {
c.authToken = authResp.Data.APIToken
}
return &authResp, nil
}