diff --git a/handlers.go b/handlers.go index 690d8e9..1615d0e 100644 --- a/handlers.go +++ b/handlers.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "os" ) // RegistrationRequest represents the incoming registration request. @@ -13,16 +14,22 @@ type RegistrationRequest struct { } func registerHandler(w http.ResponseWriter, r *http.Request) { - // Allow only POST method + // Allow only POST requests if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } - // Parse the request body into the RegistrationRequest struct + // Parse the request body var req RegistrationRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, "Invalid request body", http.StatusBadRequest) + http.Error(w, fmt.Sprintf("Invalid request body: %v", err), http.StatusBadRequest) + return + } + + // Validate email + if !validateEmailDomain(req.Email) { + http.Error(w, "Email domain is not allowed", http.StatusBadRequest) return } @@ -38,11 +45,21 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { } // Initialize Overleaf instance - overleaf := NewOverleaf("http://localhost:3000") // Replace with the actual Overleaf base URL + 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 + } - // 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 @@ -55,6 +72,9 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { } // Respond with success + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte("User registered successfully. Please check your email to set your password.")) + json.NewEncoder(w).Encode(map[string]string{ + "message": "User registered successfully. Please check your email to set your password.", + }) }