Add logic to able to prefill DEFAULT_EMAIL

This commit is contained in:
2025-08-05 21:24:49 +10:00
parent d90829974d
commit 970fece081
5 changed files with 43 additions and 1 deletions
+6
View File
@@ -65,6 +65,11 @@ auth:
# Can also be set via ADMIN_EMAIL environment variable # Can also be set via ADMIN_EMAIL environment variable
admin_email: "" # Set this to your admin email address admin_email: "" # Set this to your admin email address
# Default email to pre-fill in the login form
# When set, this email will be automatically filled in the email input field
# Leave empty to show an empty email field
default_email: "" # Set this to pre-fill the login form with a default email
# Environment-specific overrides can be set via environment variables: # Environment-specific overrides can be set via environment variables:
# - PORT: Server port # - PORT: Server port
# - HOST: Server host # - HOST: Server host
@@ -74,3 +79,4 @@ auth:
# - ALLOWED_EMAILS: Comma-separated list of allowed emails # - ALLOWED_EMAILS: Comma-separated list of allowed emails
# - ADMIN_EMAIL: Admin email address # - ADMIN_EMAIL: Admin email address
# - COOKIE_DOMAIN: Cookie domain for session cookies # - COOKIE_DOMAIN: Cookie domain for session cookies
# - DEFAULT_EMAIL: Default email to pre-fill in login form
+4
View File
@@ -42,6 +42,7 @@ type AuthConfig struct {
AllowedEmails []string `yaml:"allowed_emails"` AllowedEmails []string `yaml:"allowed_emails"`
AdminEmail string `yaml:"admin_email"` AdminEmail string `yaml:"admin_email"`
CookieDomain string `yaml:"cookie_domain"` CookieDomain string `yaml:"cookie_domain"`
DefaultEmail string `yaml:"default_email"`
} }
func Load() (*Config, error) { func Load() (*Config, error) {
@@ -122,6 +123,9 @@ func Load() (*Config, error) {
if cookieDomain := os.Getenv("COOKIE_DOMAIN"); cookieDomain != "" { if cookieDomain := os.Getenv("COOKIE_DOMAIN"); cookieDomain != "" {
config.Auth.CookieDomain = cookieDomain config.Auth.CookieDomain = cookieDomain
} }
if defaultEmail := os.Getenv("DEFAULT_EMAIL"); defaultEmail != "" {
config.Auth.DefaultEmail = defaultEmail
}
return config, nil return config, nil
} }
+8
View File
@@ -469,6 +469,14 @@ func (h *Handlers) GetAuthStatus(w http.ResponseWriter, r *http.Request) {
h.writeJSON(w, response) h.writeJSON(w, response)
} }
// GetConfig returns public configuration data
func (h *Handlers) GetConfig(w http.ResponseWriter, r *http.Request) {
configData := map[string]interface{}{
"default_email": h.config.Auth.DefaultEmail,
}
h.writeJSON(w, configData)
}
// Admin endpoints // Admin endpoints
// ListUsers returns all users (admin endpoint) // ListUsers returns all users (admin endpoint)
+1
View File
@@ -49,6 +49,7 @@ func main() {
// API routes // API routes
api := router.PathPrefix("/api").Subrouter() api := router.PathPrefix("/api").Subrouter()
api.HandleFunc("/config", h.GetConfig).Methods("GET")
api.HandleFunc("/register/begin", h.BeginRegistration).Methods("POST") api.HandleFunc("/register/begin", h.BeginRegistration).Methods("POST")
api.HandleFunc("/register/finish", h.FinishRegistration).Methods("POST") api.HandleFunc("/register/finish", h.FinishRegistration).Methods("POST")
api.HandleFunc("/login/begin", h.BeginLogin).Methods("POST") api.HandleFunc("/login/begin", h.BeginLogin).Methods("POST")
+24 -1
View File
@@ -282,7 +282,7 @@
return emailParam; return emailParam;
} }
// Pre-fill email input if email parameter exists // Pre-fill email input if email parameter exists or from config
function prefillEmail() { function prefillEmail() {
const email = getEmailFromUrl(); const email = getEmailFromUrl();
if (email) { if (email) {
@@ -290,6 +290,29 @@
if (emailInput) { if (emailInput) {
emailInput.value = decodeURIComponent(email); emailInput.value = decodeURIComponent(email);
} }
} else {
// If no email parameter, try to get default email from config
loadDefaultEmail();
}
}
// Load default email from server configuration
async function loadDefaultEmail() {
try {
const response = await fetch('/api/config');
if (response.ok) {
const config = await response.json();
if (config.default_email) {
const emailInput = document.getElementById('email');
if (emailInput && !emailInput.value) {
emailInput.value = config.default_email;
console.log('Pre-filled email from config:', config.default_email);
}
}
}
} catch (error) {
console.log('Could not load default email from config:', error);
// Fail silently - this is not critical functionality
} }
} }