// Global Auth Helper window.GlobalAuth = { // Get auth token from cookies getAuthTokenFromCookie: function() { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); if (cookie.startsWith('auth_token=')) { return cookie.substring('auth_token='.length, cookie.length); } } return null; }, // Check authentication status checkAuth: function(callback) { const token = this.getAuthTokenFromCookie(); const headers = {}; if (token) { headers['Authorization'] = 'Bearer ' + token; } fetch('https://auth.legierski.net/api/validate', { method: 'GET', headers: headers, credentials: 'include' }) .then(response => { if (!response.ok) throw new Error('Not authenticated'); return response.json(); }) .then(data => { callback(null, data); }) .catch(error => { callback(error, null); }); }, // Create Modal _createAuthModal: function() { // Remove any existing modal const existingModal = document.getElementById('global-auth-modal'); if (existingModal) { document.body.removeChild(existingModal); } // Create modal container const modal = document.createElement('div'); modal.id = 'global-auth-modal'; modal.style.position = 'fixed'; modal.style.top = '0'; modal.style.left = '0'; modal.style.width = '100%'; modal.style.height = '100%'; modal.style.backgroundColor = 'rgba(0, 0, 0, 0.6)'; modal.style.display = 'flex'; modal.style.justifyContent = 'center'; modal.style.alignItems = 'center'; modal.style.zIndex = '10000'; // Create modal content const content = document.createElement('div'); content.style.backgroundColor = 'white'; content.style.borderRadius = '8px'; content.style.width = '90%'; content.style.maxWidth = '400px'; content.style.padding = '20px'; content.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)'; // Add close button const closeBtn = document.createElement('button'); closeBtn.innerHTML = '×'; closeBtn.style.float = 'right'; closeBtn.style.border = 'none'; closeBtn.style.background = 'none'; closeBtn.style.fontSize = '24px'; closeBtn.style.cursor = 'pointer'; closeBtn.onclick = function() { document.body.removeChild(modal); }; // Add title const title = document.createElement('h2'); title.innerText = 'Login'; title.style.margin = '0 0 20px 0'; // Add form const form = document.createElement('form'); form.onsubmit = function(e) { e.preventDefault(); const emailInput = document.getElementById('global-auth-email'); const passwordInput = document.getElementById('global-auth-password'); const submitBtn = document.getElementById('global-auth-submit'); submitBtn.disabled = true; submitBtn.innerText = 'Logging in...'; window.GlobalAuth._submitLogin(emailInput.value, passwordInput.value, function(error) { if (error) { alert('Login failed: ' + error); submitBtn.disabled = false; submitBtn.innerText = 'Login'; } else { document.body.removeChild(modal); // Refresh the page or call a success callback window.location.reload(); } }); }; // Add email input const emailLabel = document.createElement('label'); emailLabel.innerText = 'Email:'; emailLabel.style.display = 'block'; emailLabel.style.marginBottom = '5px'; const emailInput = document.createElement('input'); emailInput.type = 'email'; emailInput.id = 'global-auth-email'; emailInput.required = true; emailInput.style.width = '100%'; emailInput.style.padding = '8px'; emailInput.style.marginBottom = '15px'; emailInput.style.boxSizing = 'border-box'; // Add password input const passwordLabel = document.createElement('label'); passwordLabel.innerText = 'Password:'; passwordLabel.style.display = 'block'; passwordLabel.style.marginBottom = '5px'; const passwordInput = document.createElement('input'); passwordInput.type = 'password'; passwordInput.id = 'global-auth-password'; passwordInput.required = true; passwordInput.style.width = '100%'; passwordInput.style.padding = '8px'; passwordInput.style.marginBottom = '20px'; passwordInput.style.boxSizing = 'border-box'; // Add submit button const submitBtn = document.createElement('button'); submitBtn.type = 'submit'; submitBtn.id = 'global-auth-submit'; submitBtn.innerText = 'Login'; submitBtn.style.backgroundColor = '#4CAF50'; submitBtn.style.color = 'white'; submitBtn.style.padding = '10px 15px'; submitBtn.style.border = 'none'; submitBtn.style.borderRadius = '4px'; submitBtn.style.cursor = 'pointer'; // Add register link const registerLink = document.createElement('a'); registerLink.innerText = 'Register new account'; registerLink.href = '#'; registerLink.style.display = 'inline-block'; registerLink.style.marginLeft = '10px'; registerLink.onclick = function(e) { e.preventDefault(); document.body.removeChild(modal); window.GlobalAuth.register(); }; // Assemble form form.appendChild(emailLabel); form.appendChild(emailInput); form.appendChild(passwordLabel); form.appendChild(passwordInput); form.appendChild(submitBtn); form.appendChild(registerLink); // Assemble modal content.appendChild(closeBtn); content.appendChild(title); content.appendChild(form); modal.appendChild(content); return modal; }, // Create Register Modal _createRegisterModal: function() { // Remove any existing modal const existingModal = document.getElementById('global-auth-modal'); if (existingModal) { document.body.removeChild(existingModal); } // Create modal container const modal = document.createElement('div'); modal.id = 'global-auth-modal'; modal.style.position = 'fixed'; modal.style.top = '0'; modal.style.left = '0'; modal.style.width = '100%'; modal.style.height = '100%'; modal.style.backgroundColor = 'rgba(0, 0, 0, 0.6)'; modal.style.display = 'flex'; modal.style.justifyContent = 'center'; modal.style.alignItems = 'center'; modal.style.zIndex = '10000'; // Create modal content const content = document.createElement('div'); content.style.backgroundColor = 'white'; content.style.borderRadius = '8px'; content.style.width = '90%'; content.style.maxWidth = '400px'; content.style.padding = '20px'; content.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)'; // Add close button const closeBtn = document.createElement('button'); closeBtn.innerHTML = '×'; closeBtn.style.float = 'right'; closeBtn.style.border = 'none'; closeBtn.style.background = 'none'; closeBtn.style.fontSize = '24px'; closeBtn.style.cursor = 'pointer'; closeBtn.onclick = function() { document.body.removeChild(modal); }; // Add title const title = document.createElement('h2'); title.innerText = 'Register'; title.style.margin = '0 0 20px 0'; // Add form const form = document.createElement('form'); form.onsubmit = function(e) { e.preventDefault(); const emailInput = document.getElementById('global-auth-email'); const passwordInput = document.getElementById('global-auth-password'); const submitBtn = document.getElementById('global-auth-submit'); submitBtn.disabled = true; submitBtn.innerText = 'Registering...'; window.GlobalAuth._submitRegister(emailInput.value, passwordInput.value, function(error) { if (error) { alert('Registration failed: ' + error); submitBtn.disabled = false; submitBtn.innerText = 'Register'; } else { alert('Registration successful! Please log in.'); document.body.removeChild(modal); window.GlobalAuth.login(); } }); }; // Add email input const emailLabel = document.createElement('label'); emailLabel.innerText = 'Email:'; emailLabel.style.display = 'block'; emailLabel.style.marginBottom = '5px'; const emailInput = document.createElement('input'); emailInput.type = 'email'; emailInput.id = 'global-auth-email'; emailInput.required = true; emailInput.style.width = '100%'; emailInput.style.padding = '8px'; emailInput.style.marginBottom = '15px'; emailInput.style.boxSizing = 'border-box'; // Add password input const passwordLabel = document.createElement('label'); passwordLabel.innerText = 'Password:'; passwordLabel.style.display = 'block'; passwordLabel.style.marginBottom = '5px'; const passwordInput = document.createElement('input'); passwordInput.type = 'password'; passwordInput.id = 'global-auth-password'; passwordInput.required = true; passwordInput.style.width = '100%'; passwordInput.style.padding = '8px'; passwordInput.style.marginBottom = '20px'; passwordInput.style.boxSizing = 'border-box'; // Add submit button const submitBtn = document.createElement('button'); submitBtn.type = 'submit'; submitBtn.id = 'global-auth-submit'; submitBtn.innerText = 'Register'; submitBtn.style.backgroundColor = '#4CAF50'; submitBtn.style.color = 'white'; submitBtn.style.padding = '10px 15px'; submitBtn.style.border = 'none'; submitBtn.style.borderRadius = '4px'; submitBtn.style.cursor = 'pointer'; // Add login link const loginLink = document.createElement('a'); loginLink.innerText = 'Login to existing account'; loginLink.href = '#'; loginLink.style.display = 'inline-block'; loginLink.style.marginLeft = '10px'; loginLink.onclick = function(e) { e.preventDefault(); document.body.removeChild(modal); window.GlobalAuth.login(); }; // Assemble form form.appendChild(emailLabel); form.appendChild(emailInput); form.appendChild(passwordLabel); form.appendChild(passwordInput); form.appendChild(submitBtn); form.appendChild(loginLink); // Assemble modal content.appendChild(closeBtn); content.appendChild(title); content.appendChild(form); modal.appendChild(content); return modal; }, // Submit login data _submitLogin: function(email, password, callback) { fetch('https://auth.legierski.net/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: email, password: password, origin: window.location.origin }), credentials: 'include' }) .then(response => { if (!response.ok) { return response.json().then(data => { throw new Error(data.error || 'Login failed'); }); } return response.json(); }) .then(data => { callback(null); }) .catch(error => { callback(error.message); }); }, // Submit registration data _submitRegister: function(email, password, callback) { fetch('https://auth.legierski.net/api/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: email, password: password }), credentials: 'include' }) .then(response => { if (!response.ok) { return response.json().then(data => { throw new Error(data.error || 'Registration failed'); }); } return response.json(); }) .then(data => { callback(null); }) .catch(error => { callback(error.message); }); }, // Show login modal instead of redirecting login: function() { const modal = this._createAuthModal(); document.body.appendChild(modal); }, // Show register modal register: function() { const modal = this._createRegisterModal(); document.body.appendChild(modal); }, // Logout - still uses redirection for simplicity logout: function() { window.location.href = 'https://auth.legierski.net/logout?redirect=' + encodeURIComponent(window.location.href); } };