140 lines
3.5 KiB
Go
140 lines
3.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
tinder "tinder-api-wrapper"
|
|
)
|
|
|
|
// HandleSendPhoneAuth handles the phone authentication request
|
|
func HandleSendPhoneAuth(client *tinder.Client) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var authReq struct {
|
|
PhoneNumber string `json:"phone_number"`
|
|
}
|
|
if err := readJSON(r, &authReq); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
resp, err := client.SendPhoneLogin(authReq.PhoneNumber)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, resp)
|
|
}
|
|
}
|
|
|
|
// HandleValidateOTP handles the OTP validation request
|
|
func HandleValidateOTP(client *tinder.Client) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var authReq struct {
|
|
PhoneNumber string `json:"phone_number"`
|
|
OTP string `json:"otp"`
|
|
}
|
|
if err := readJSON(r, &authReq); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
resp, err := client.LoginWithOTP(authReq.PhoneNumber, authReq.OTP)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, resp)
|
|
}
|
|
}
|
|
|
|
// HandleFacebookAuth handles Facebook authentication
|
|
func HandleFacebookAuth(client *tinder.Client) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var authReq struct {
|
|
FacebookID string `json:"facebook_id"`
|
|
FacebookToken string `json:"facebook_token"`
|
|
}
|
|
if err := readJSON(r, &authReq); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
resp, err := client.LoginWithFacebook(authReq.FacebookID, authReq.FacebookToken)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, resp)
|
|
}
|
|
}
|
|
|
|
// HandleRefreshAuth handles authentication token refresh
|
|
func HandleRefreshAuth(client *tinder.Client) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var authReq struct {
|
|
RefreshToken string `json:"refresh_token"`
|
|
}
|
|
if err := readJSON(r, &authReq); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
resp, err := client.RefreshAuth(authReq.RefreshToken)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, resp)
|
|
}
|
|
}
|
|
|
|
// HandleGetProfile handles profile retrieval
|
|
func HandleGetProfile(client *tinder.Client) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
authToken := r.Header.Get("X-Auth-Token")
|
|
if authToken == "" {
|
|
http.Error(w, "Missing authentication token", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
clientWithAuth := client.WithAuthToken(authToken)
|
|
resp, err := clientWithAuth.GetProfile()
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("API error: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, resp)
|
|
}
|
|
}
|