|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Passkey Authentication</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + font-family: system-ui, -apple-system, sans-serif; |
| 10 | + display: flex; |
| 11 | + align-items: center; |
| 12 | + justify-content: center; |
| 13 | + min-height: 100vh; |
| 14 | + margin: 0; |
| 15 | + background: #f8f9fa; |
| 16 | + } |
| 17 | + .container { |
| 18 | + text-align: center; |
| 19 | + max-width: 400px; |
| 20 | + padding: 2rem; |
| 21 | + background: white; |
| 22 | + border-radius: 8px; |
| 23 | + box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
| 24 | + } |
| 25 | + .spinner { |
| 26 | + width: 40px; |
| 27 | + height: 40px; |
| 28 | + border: 4px solid #f3f3f3; |
| 29 | + border-top: 4px solid #007bff; |
| 30 | + border-radius: 50%; |
| 31 | + animation: spin 1s linear infinite; |
| 32 | + margin: 0 auto 1rem; |
| 33 | + } |
| 34 | + @keyframes spin { |
| 35 | + 0% { transform: rotate(0deg); } |
| 36 | + 100% { transform: rotate(360deg); } |
| 37 | + } |
| 38 | + </style> |
| 39 | +</head> |
| 40 | +<body> |
| 41 | + <div class="container"> |
| 42 | + <div class="spinner"></div> |
| 43 | + <h2>🔐 Passkey Authentication</h2> |
| 44 | + <p id="status">Checking authentication status...</p> |
| 45 | + </div> |
| 46 | + |
| 47 | + <script> |
| 48 | + function updateStatus(message) { |
| 49 | + document.getElementById('status').textContent = message; |
| 50 | + } |
| 51 | + |
| 52 | + function getRedirectUrl() { |
| 53 | + const urlParams = new URLSearchParams(window.location.search); |
| 54 | + // Check for both 'redirect' and 'rd' parameters (nginx uses 'rd' by default) |
| 55 | + // Prefer 'redirect' over 'rd' if both are present |
| 56 | + const redirectParam = urlParams.get('redirect'); |
| 57 | + const rdParam = urlParams.get('rd'); |
| 58 | + |
| 59 | + console.log('URL params:', { |
| 60 | + redirect: redirectParam, |
| 61 | + rd: rdParam, |
| 62 | + search: window.location.search |
| 63 | + }); |
| 64 | + |
| 65 | + return redirectParam || rdParam; |
| 66 | + } |
| 67 | + |
| 68 | + function redirectToTarget(url) { |
| 69 | + try { |
| 70 | + const decodedUrl = decodeURIComponent(url); |
| 71 | + updateStatus(`Redirecting to ${new URL(decodedUrl).hostname}...`); |
| 72 | + console.log('Redirecting to:', decodedUrl); |
| 73 | + // Small delay to show the message |
| 74 | + setTimeout(() => { |
| 75 | + window.location.href = decodedUrl; |
| 76 | + }, 1000); |
| 77 | + return true; |
| 78 | + } catch (error) { |
| 79 | + console.error('Error processing redirect URL:', error); |
| 80 | + updateStatus('Invalid redirect URL'); |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function redirectToLogin() { |
| 86 | + const redirectUrl = getRedirectUrl(); |
| 87 | + if (redirectUrl) { |
| 88 | + // Preserve the redirect parameter when going to login |
| 89 | + const loginUrl = `/login.html?redirect=${encodeURIComponent(redirectUrl)}`; |
| 90 | + updateStatus('Redirecting to login...'); |
| 91 | + setTimeout(() => { |
| 92 | + window.location.href = loginUrl; |
| 93 | + }, 1000); |
| 94 | + } else { |
| 95 | + // No redirect parameter, just go to login |
| 96 | + window.location.href = '/login.html'; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + async function checkAuthAndRedirect() { |
| 101 | + const redirectUrl = getRedirectUrl(); |
| 102 | + |
| 103 | + if (!redirectUrl) { |
| 104 | + updateStatus('No redirect URL provided'); |
| 105 | + setTimeout(() => { |
| 106 | + window.location.href = '/login.html'; |
| 107 | + }, 2000); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + updateStatus('Checking authentication...'); |
| 113 | + |
| 114 | + const response = await fetch('/api/auth/status', { |
| 115 | + credentials: 'include' |
| 116 | + }); |
| 117 | + |
| 118 | + if (response.ok) { |
| 119 | + const userData = await response.json(); |
| 120 | + if (userData.authenticated) { |
| 121 | + updateStatus(`Welcome back, ${userData.user.display_name}!`); |
| 122 | + redirectToTarget(redirectUrl); |
| 123 | + return; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Not authenticated, redirect to login with the target URL |
| 128 | + updateStatus('Authentication required...'); |
| 129 | + redirectToLogin(); |
| 130 | + |
| 131 | + } catch (error) { |
| 132 | + console.error('Auth check failed:', error); |
| 133 | + updateStatus('Authentication check failed...'); |
| 134 | + redirectToLogin(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Start the process when page loads |
| 139 | + document.addEventListener('DOMContentLoaded', checkAuthAndRedirect); |
| 140 | + </script> |
| 141 | +</body> |
| 142 | +</html> |
0 commit comments