mirror of
https://github.com/ssep1ol/overleaf-registration-be.git
synced 2026-07-21 12:30:21 +00:00
First commit
This commit is contained in:
71
handlers.go
71
handlers.go
@@ -2,72 +2,59 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RegisterRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Captcha string `json:"captcha"`
|
||||
// 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) {
|
||||
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
|
||||
var req RegisterRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
// 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 email domain
|
||||
allowedDomains := strings.Split(os.Getenv("ALLOWED_DOMAINS"), ",")
|
||||
if !isDomainAllowed(req.Email, allowedDomains) {
|
||||
http.Error(w, "Email domain not allowed", http.StatusForbidden)
|
||||
// Validate reCAPTCHA token
|
||||
valid, err := validateRecaptcha(req.Captcha)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("CAPTCHA validation failed: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate CAPTCHA
|
||||
if !ValidateCaptcha(req.Captcha) {
|
||||
if !valid {
|
||||
http.Error(w, "Invalid CAPTCHA", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Register user
|
||||
overleaf := NewOverleaf(os.Getenv("OL_INSTANCE"))
|
||||
err = overleaf.Login(os.Getenv("OL_ADMIN_EMAIL"), os.Getenv("OL_ADMIN_PASSWORD"))
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to log in as admin", http.StatusInternalServerError)
|
||||
// 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
|
||||
}
|
||||
|
||||
err = overleaf.RegisterUser(req.Email, req.Password)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to register user: "+err.Error(), http.StatusInternalServerError)
|
||||
// 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
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "User registered successfully"})
|
||||
}
|
||||
|
||||
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
|
||||
// Respond with success
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("User registered successfully. Please check your email to set your password."))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user