Initialize Tinder API Wrapper with server configuration and Docker setup
This commit is contained in:
139
cmd/server/handlers/auth.go
Normal file
139
cmd/server/handlers/auth.go
Normal file
@@ -0,0 +1,139 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user