Update index.html
1 file changed+254−17
index.html+254−17View file
| @@ -1924,35 +1924,182 @@ | ||
| } | ||
| } | ||
| - function setAuthModalMode(mode) { | ||
| + async function handleAuthForgotSubmit(e) { | ||
| + e.preventDefault(); | ||
| clearAuthError(); | ||
| + clearAuthNotice(); | ||
| + const email = document.getElementById('auth-forgot-email').value.trim(); | ||
| + const submitBtn = document.getElementById('auth-forgot-submit-btn'); | ||
| + | ||
| + if (!email) { | ||
| + showAuthError('Please enter your email address.'); | ||
| + return; | ||
| + } | ||
| + | ||
| + const origText = submitBtn.innerHTML; | ||
| + submitBtn.disabled = true; | ||
| + submitBtn.innerHTML = ` | ||
| + <svg class="animate-spin w-4 h-4 text-white" fill="none" viewBox="0 0 24 24"> | ||
| + <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle> | ||
| + <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8H4z"></path> | ||
| + </svg> | ||
| + <span>Sending request...</span> | ||
| + `; | ||
| + | ||
| + try { | ||
| + const res = await fetch(`/api/p/${PROJECT_ID}/auth/password-reset/request`, { | ||
| + method: 'POST', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ email }) | ||
| + }); | ||
| + | ||
| + if (res.ok) { | ||
| + showAuthNotice('Password Reset Link Sent', `If an account exists for ${email}, a password reset link has been dispatched to your email address. Please check your inbox and follow the instructions to reset your password.`); | ||
| + showToast('Reset Link Sent', 'Check your email for reset instructions.', 'success'); | ||
| + document.getElementById('auth-forgot-email').value = ''; | ||
| + } else { | ||
| + const errData = await res.json().catch(() => ({})); | ||
| + showAuthError(errData.message || 'Unable to submit password reset request. Please check the email address.'); | ||
| + } | ||
| + } catch (err) { | ||
| + showAuthError(err.message || 'Failed to submit reset request. Please check your network connection.'); | ||
| + } finally { | ||
| + submitBtn.disabled = false; | ||
| + submitBtn.innerHTML = origText; | ||
| + } | ||
| + } | ||
| + | ||
| + async function handleAuthResetSubmit(e) { | ||
| + e.preventDefault(); | ||
| + clearAuthError(); | ||
| + clearAuthNotice(); | ||
| + const token = document.getElementById('auth-reset-token').value.trim(); | ||
| + const newPassword = document.getElementById('auth-reset-password').value; | ||
| + const confirmPassword = document.getElementById('auth-reset-password-confirm').value; | ||
| + const submitBtn = document.getElementById('auth-reset-submit-btn'); | ||
| + | ||
| + if (!token) { | ||
| + showAuthError('Reset token is missing or invalid. Please request a new reset link.'); | ||
| + return; | ||
| + } | ||
| + if (!newPassword || newPassword.length < 6) { | ||
| + showAuthError('Password must be at least 6 characters in length.'); | ||
| + return; | ||
| + } | ||
| + if (newPassword !== confirmPassword) { | ||
| + showAuthError('Passwords do not match. Please verify and re-enter.'); | ||
| + return; | ||
| + } | ||
| + | ||
| + const origText = submitBtn.innerHTML; | ||
| + submitBtn.disabled = true; | ||
| + submitBtn.innerHTML = ` | ||
| + <svg class="animate-spin w-4 h-4 text-white" fill="none" viewBox="0 0 24 24"> | ||
| + <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle> | ||
| + <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8H4z"></path> | ||
| + </svg> | ||
| + <span>Updating password...</span> | ||
| + `; | ||
| + | ||
| + try { | ||
| + const res = await fetch(`/api/p/${PROJECT_ID}/auth/password-reset/confirm`, { | ||
| + method: 'POST', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ token, newPassword }) | ||
| + }); | ||
| + | ||
| + if (res.ok) { | ||
| + document.getElementById('auth-reset-password').value = ''; | ||
| + document.getElementById('auth-reset-password-confirm').value = ''; | ||
| + document.getElementById('auth-reset-token').value = ''; | ||
| + setAuthModalMode('login', true); | ||
| + showAuthNotice('Password Reset Successful', 'Your password has been reset successfully! You can now sign in with your new password.'); | ||
| + showToast('Password Updated', 'Your password was successfully updated.', 'success'); | ||
| + if (window.history && window.history.replaceState) { | ||
| + window.history.replaceState(null, '', window.location.pathname + window.location.search); | ||
| + } else { | ||
| + window.location.hash = ''; | ||
| + } | ||
| + } else { | ||
| + const errData = await res.json().catch(() => ({})); | ||
| + showAuthError(errData.message || 'Failed to reset password. The link may have expired or is invalid.'); | ||
| + showToast('Reset Failed', errData.message || 'Failed to reset password.', 'error'); | ||
| + } | ||
| + } catch (err) { | ||
| + showAuthError(err.message || 'Failed to communicate with server. Please try again.'); | ||
| + } finally { | ||
| + submitBtn.disabled = false; | ||
| + submitBtn.innerHTML = origText; | ||
| + } | ||
| + } | ||
| + | ||
| + function setAuthModalMode(mode, keepNotice = false) { | ||
| + clearAuthError(); | ||
| + if (!keepNotice) clearAuthNotice(); | ||
| const loginForm = document.getElementById('form-auth-login'); | ||
| const regForm = document.getElementById('form-auth-register'); | ||
| + const forgotForm = document.getElementById('form-auth-forgot'); | ||
| + const resetForm = document.getElementById('form-auth-reset'); | ||
| + const demoSection = document.getElementById('auth-demo-section'); | ||
| + const tabsSection = document.getElementById('auth-mode-tabs'); | ||
| const loginTab = document.getElementById('tab-auth-login'); | ||
| const regTab = document.getElementById('tab-auth-register'); | ||
| const title = document.getElementById('auth-modal-title'); | ||
| const desc = document.getElementById('auth-modal-desc'); | ||
| - if (mode === 'register') { | ||
| - loginForm.classList.add('hidden'); | ||
| - regForm.classList.remove('hidden'); | ||
| - loginTab.className = "py-1.5 text-xs font-bold rounded-lg text-slate-600 hover:text-slate-900 transition"; | ||
| - regTab.className = "py-1.5 text-xs font-bold rounded-lg text-indigo-700 bg-white shadow-sm transition"; | ||
| - title.textContent = "Create an Account"; | ||
| - desc.textContent = "Join as a Freelancer or Project Administrator."; | ||
| + if (mode === 'forgot') { | ||
| + if (demoSection) demoSection.classList.add('hidden'); | ||
| + if (tabsSection) tabsSection.classList.add('hidden'); | ||
| + if (loginForm) loginForm.classList.add('hidden'); | ||
| + if (regForm) regForm.classList.add('hidden'); | ||
| + if (resetForm) resetForm.classList.add('hidden'); | ||
| + if (forgotForm) forgotForm.classList.remove('hidden'); | ||
| + if (title) title.textContent = "Forgot Password"; | ||
| + if (desc) desc.textContent = "Enter your email to receive a password reset link."; | ||
| + const currentEmail = document.getElementById('auth-login-email')?.value?.trim(); | ||
| + const forgotEmailInput = document.getElementById('auth-forgot-email'); | ||
| + if (forgotEmailInput && currentEmail) forgotEmailInput.value = currentEmail; | ||
| + } else if (mode === 'reset') { | ||
| + if (demoSection) demoSection.classList.add('hidden'); | ||
| + if (tabsSection) tabsSection.classList.add('hidden'); | ||
| + if (loginForm) loginForm.classList.add('hidden'); | ||
| + if (regForm) regForm.classList.add('hidden'); | ||
| + if (forgotForm) forgotForm.classList.add('hidden'); | ||
| + if (resetForm) resetForm.classList.remove('hidden'); | ||
| + if (title) title.textContent = "Reset Password"; | ||
| + if (desc) desc.textContent = "Create a new password for your account."; | ||
| + } else if (mode === 'register') { | ||
| + if (demoSection) demoSection.classList.remove('hidden'); | ||
| + if (tabsSection) tabsSection.classList.remove('hidden'); | ||
| + if (loginForm) loginForm.classList.add('hidden'); | ||
| + if (forgotForm) forgotForm.classList.add('hidden'); | ||
| + if (resetForm) resetForm.classList.add('hidden'); | ||
| + if (regForm) regForm.classList.remove('hidden'); | ||
| + if (loginTab) loginTab.className = "py-1.5 text-xs font-bold rounded-lg text-slate-600 hover:text-slate-900 transition"; | ||
| + if (regTab) regTab.className = "py-1.5 text-xs font-bold rounded-lg text-indigo-700 bg-white shadow-sm transition"; | ||
| + if (title) title.textContent = "Create an Account"; | ||
| + if (desc) desc.textContent = "Join as a Freelancer or Project Administrator."; | ||
| } else { | ||
| - regForm.classList.add('hidden'); | ||
| - loginForm.classList.remove('hidden'); | ||
| - loginTab.className = "py-1.5 text-xs font-bold rounded-lg text-indigo-700 bg-white shadow-sm transition"; | ||
| - regTab.className = "py-1.5 text-xs font-bold rounded-lg text-slate-600 hover:text-slate-900 transition"; | ||
| - title.textContent = "Sign In to Timesheets Pro"; | ||
| - desc.textContent = "Access your timesheet portal and project dashboard."; | ||
| + // default 'login' | ||
| + if (demoSection) demoSection.classList.remove('hidden'); | ||
| + if (tabsSection) tabsSection.classList.remove('hidden'); | ||
| + if (regForm) regForm.classList.add('hidden'); | ||
| + if (forgotForm) forgotForm.classList.add('hidden'); | ||
| + if (resetForm) resetForm.classList.add('hidden'); | ||
| + if (loginForm) loginForm.classList.remove('hidden'); | ||
| + if (loginTab) loginTab.className = "py-1.5 text-xs font-bold rounded-lg text-indigo-700 bg-white shadow-sm transition"; | ||
| + if (regTab) regTab.className = "py-1.5 text-xs font-bold rounded-lg text-slate-600 hover:text-slate-900 transition"; | ||
| + if (title) title.textContent = "Sign In to Timesheets Pro"; | ||
| + if (desc) desc.textContent = "Access your timesheet portal and project dashboard."; | ||
| } | ||
| } | ||
| - function openAuthModal(mode = 'login') { | ||
| - clearAuthError(); | ||
| - setAuthModalMode(mode); | ||
| + function openAuthModal(mode = 'login', token = null) { | ||
| + setAuthModalMode(mode, true); | ||
| + if (mode === 'reset' && token) { | ||
| + const tokenInput = document.getElementById('auth-reset-token'); | ||
| + if (tokenInput) tokenInput.value = token; | ||
| + } | ||
| const modal = document.getElementById('modal-auth'); | ||
| if (modal) modal.classList.remove('hidden'); | ||
| } | ||
| @@ -1962,6 +2109,22 @@ | ||
| if (modal) modal.classList.add('hidden'); | ||
| } | ||
| + function showAuthNotice(title, msg) { | ||
| + const alert = document.getElementById('auth-notice-alert'); | ||
| + const titleEl = document.getElementById('auth-notice-title'); | ||
| + const msgEl = document.getElementById('auth-notice-msg'); | ||
| + if (alert && titleEl && msgEl) { | ||
| + titleEl.textContent = title; | ||
| + msgEl.textContent = msg; | ||
| + alert.classList.remove('hidden'); | ||
| + } | ||
| + } | ||
| + | ||
| + function clearAuthNotice() { | ||
| + const alert = document.getElementById('auth-notice-alert'); | ||
| + if (alert) alert.classList.add('hidden'); | ||
| + } | ||
| + | ||
| function showAuthError(msg) { | ||
| const alert = document.getElementById('auth-error-alert'); | ||
| const text = document.getElementById('auth-error-msg'); | ||
| @@ -1976,6 +2139,80 @@ | ||
| if (alert) alert.classList.add('hidden'); | ||
| } | ||
| + // ------------------------------------------------------------- | ||
| + // URL Hash Router for Auth Actions (Verify Email & Reset Password) | ||
| + // ------------------------------------------------------------- | ||
| + async function handleHashRoute() { | ||
| + const hash = window.location.hash || ''; | ||
| + if (!hash || !hash.includes('auth/')) return; | ||
| + | ||
| + const raw = hash.replace(/^#\/?/, ''); | ||
| + const [pathPart, queryPart] = raw.split('?'); | ||
| + const normalizedPath = (pathPart || '').toLowerCase().replace(/^\/+|\/+$/g, ''); | ||
| + | ||
| + const params = new URLSearchParams(queryPart || ''); | ||
| + if (window.location.search) { | ||
| + const searchParams = new URLSearchParams(window.location.search); | ||
| + for (const [k, v] of searchParams.entries()) { | ||
| + if (!params.has(k)) params.set(k, v); | ||
| + } | ||
| + } | ||
| + | ||
| + let token = params.get('token') || ''; | ||
| + if (!token) { | ||
| + const match = (hash + window.location.search).match(/[?&]token=([^&]+)/); | ||
| + if (match) token = decodeURIComponent(match[1]); | ||
| + } | ||
| + | ||
| + if (normalizedPath === 'auth/verify-email') { | ||
| + openAuthModal('login', null); | ||
| + if (!token) { | ||
| + showAuthError('Email verification token is missing. Please check your verification link.'); | ||
| + return; | ||
| + } | ||
| + | ||
| + showAuthNotice('Verifying Email...', 'Please wait while we verify your email address.'); | ||
| + try { | ||
| + const res = await fetch(`/api/p/${PROJECT_ID}/auth/verify-email`, { | ||
| + method: 'POST', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ token }) | ||
| + }); | ||
| + | ||
| + if (res.ok) { | ||
| + clearAuthError(); | ||
| + showAuthNotice('Email Verified Successfully', 'Your email address has been verified! You can now sign in with your credentials.'); | ||
| + showToast('Email Verified', 'Email verified successfully! Please sign in.', 'success'); | ||
| + if (window.history && window.history.replaceState) { | ||
| + window.history.replaceState(null, '', window.location.pathname); | ||
| + } else { | ||
| + window.location.hash = ''; | ||
| + } | ||
| + } else { | ||
| + clearAuthNotice(); | ||
| + const errData = await res.json().catch(() => ({})); | ||
| + showAuthError(errData.message || 'Email verification failed or token has expired.'); | ||
| + showToast('Verification Failed', errData.message || 'Verification token invalid or expired.', 'error'); | ||
| + } | ||
| + } catch (err) { | ||
| + clearAuthNotice(); | ||
| + showAuthError(err.message || 'Failed to connect to authentication server. Please try again.'); | ||
| + } | ||
| + } else if (normalizedPath === 'auth/reset-password') { | ||
| + if (!token) { | ||
| + openAuthModal('login', null); | ||
| + showAuthError('Password reset token is missing or invalid. Please request a new reset link.'); | ||
| + return; | ||
| + } | ||
| + | ||
| + openAuthModal('reset', token); | ||
| + } | ||
| + } | ||
| + | ||
| + window.addEventListener('hashchange', () => { | ||
| + handleHashRoute(); | ||
| + }); | ||
| + | ||
| function toggleDemoDropdown(event) { | ||
| if (event) event.stopPropagation(); | ||
| const menu = document.getElementById('demo-dropdown-menu'); | ||