mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 20:40:21 +00:00
First commit
This commit is contained in:
44
handlers.go
44
handlers.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -15,12 +16,6 @@ type RegistrationRequest struct {
|
||||
}
|
||||
|
||||
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Allow only POST requests
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the request body
|
||||
var req RegistrationRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -28,7 +23,10 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate email
|
||||
log.Printf("Received email: %s", req.Email)
|
||||
log.Printf("Received CAPTCHA token: %s", req.Captcha)
|
||||
|
||||
// Validate email domain
|
||||
if !validateEmailDomain(req.Email) {
|
||||
http.Error(w, "Email domain is not allowed", http.StatusBadRequest)
|
||||
return
|
||||
@@ -37,43 +35,17 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Validate Turnstile token
|
||||
valid, err := validateRecaptcha(req.Captcha)
|
||||
if err != nil {
|
||||
log.Printf("CAPTCHA validation failed: %v", err)
|
||||
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !valid {
|
||||
log.Println("Invalid CAPTCHA token.")
|
||||
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize Overleaf instance
|
||||
baseURL := os.Getenv("OL_INSTANCE")
|
||||
if baseURL == "" {
|
||||
http.Error(w, "OL_INSTANCE is not set", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
overleaf := NewOverleaf(baseURL)
|
||||
|
||||
// Admin login using environment variables
|
||||
adminEmail := os.Getenv("OL_ADMIN_EMAIL")
|
||||
adminPassword := os.Getenv("OL_ADMIN_PASSWORD")
|
||||
if adminEmail == "" || adminPassword == "" {
|
||||
http.Error(w, "OL_ADMIN_EMAIL or OL_ADMIN_PASSWORD is not set", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
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.Header().Set("Content-Type", "application/json")
|
||||
// Continue with Overleaf registration (omitted for brevity)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"message": "User registered successfully. Please check your email to set your password.",
|
||||
|
||||
Reference in New Issue
Block a user