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:
36
handlers.go
36
handlers.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegistrationRequest represents the incoming registration request.
|
// RegistrationRequest represents the incoming registration request.
|
||||||
@@ -13,16 +14,22 @@ type RegistrationRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Allow only POST method
|
// Allow only POST requests
|
||||||
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 body into the RegistrationRequest struct
|
// Parse the request body
|
||||||
var req RegistrationRequest
|
var req RegistrationRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,11 +45,21 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Overleaf instance
|
// 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 {
|
if err := overleaf.Login(adminEmail, adminPassword); err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Admin login failed: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -55,6 +72,9 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Respond with success
|
// Respond with success
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
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.",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user