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:
67
handlers.go
67
handlers.go
@@ -2,72 +2,59 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RegisterRequest struct {
|
// RegistrationRequest represents the incoming registration request.
|
||||||
|
type RegistrationRequest struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
|
||||||
Captcha string `json:"captcha"`
|
Captcha string `json:"captcha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Allow only POST method
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the request
|
// Parse the request body into the RegistrationRequest struct
|
||||||
var req RegisterRequest
|
var req RegistrationRequest
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate email domain
|
// Validate reCAPTCHA token
|
||||||
allowedDomains := strings.Split(os.Getenv("ALLOWED_DOMAINS"), ",")
|
valid, err := validateRecaptcha(req.Captcha)
|
||||||
if !isDomainAllowed(req.Email, allowedDomains) {
|
if err != nil {
|
||||||
http.Error(w, "Email domain not allowed", http.StatusForbidden)
|
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !valid {
|
||||||
// Validate CAPTCHA
|
|
||||||
if !ValidateCaptcha(req.Captcha) {
|
|
||||||
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
|
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register user
|
// Initialize Overleaf instance
|
||||||
overleaf := NewOverleaf(os.Getenv("OL_INSTANCE"))
|
overleaf := NewOverleaf("http://localhost:3000") // Replace with the actual Overleaf base URL
|
||||||
err = overleaf.Login(os.Getenv("OL_ADMIN_EMAIL"), os.Getenv("OL_ADMIN_PASSWORD"))
|
|
||||||
if err != nil {
|
// Optional: Log in as admin (if needed for user creation)
|
||||||
http.Error(w, "Failed to log in as admin", http.StatusInternalServerError)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = overleaf.RegisterUser(req.Email, req.Password)
|
// Create the user in Overleaf
|
||||||
if err != nil {
|
if err := overleaf.RegisterUser(req.Email); err != nil {
|
||||||
http.Error(w, "Failed to register user: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Failed to create user: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
// Respond with success
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": "User registered successfully"})
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
w.Write([]byte("User registered successfully. Please check your email to set your password."))
|
||||||
|
|
||||||
func isDomainAllowed(email string, allowedDomains []string) bool {
|
|
||||||
parts := strings.Split(email, "@")
|
|
||||||
if len(parts) != 2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
domain := parts[1]
|
|
||||||
for _, allowed := range allowedDomains {
|
|
||||||
if domain == allowed {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user