Files
overleaf-registration-be/handlers.go
corrado.mulas 9d22e4d3a3 First commit
2024-11-20 00:17:54 +01:00

61 lines
1.8 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
)
// RegistrationRequest represents the incoming registration request.
type RegistrationRequest struct {
Email string `json:"email"`
Captcha string `json:"captcha"`
}
func registerHandler(w http.ResponseWriter, r *http.Request) {
// Allow only POST method
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse the request body into the RegistrationRequest struct
var req RegistrationRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Validate reCAPTCHA token
valid, err := validateRecaptcha(req.Captcha)
if err != nil {
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
return
}
if !valid {
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
return
}
// Initialize Overleaf instance
overleaf := NewOverleaf("http://localhost:3000") // Replace with the actual Overleaf base URL
// Optional: Log in as admin (if needed for user creation)
adminEmail := "admin@example.com" // Replace with actual admin email
adminPassword := "adminpassword" // Replace with actual admin password
if err := overleaf.Login(adminEmail, adminPassword); err != nil {
http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError)
return
}
// Create the user in Overleaf
if err := overleaf.RegisterUser(req.Email); err != nil {
http.Error(w, fmt.Sprintf("Failed to create user: %v", err), http.StatusInternalServerError)
return
}
// Respond with success
w.WriteHeader(http.StatusOK)
w.Write([]byte("User registered successfully. Please check your email to set your password."))
}