mirror of
https://github.com/wahyd4/passkey-auth.git
synced 2026-08-08 20:15:44 +10:00
143 lines
4.7 KiB
HTML
143 lines
4.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Passkey Auth</title>
|
|
<style>
|
|
body {
|
|
font-family: system-ui, -apple-system, sans-serif;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 100vh;
|
|
margin: 0;
|
|
background: #f8f9fa;
|
|
}
|
|
.container {
|
|
text-align: center;
|
|
max-width: 400px;
|
|
padding: 2rem;
|
|
background: white;
|
|
border-radius: 8px;
|
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
|
}
|
|
.spinner {
|
|
width: 40px;
|
|
height: 40px;
|
|
border: 4px solid #f3f3f3;
|
|
border-top: 4px solid #007bff;
|
|
border-radius: 50%;
|
|
animation: spin 1s linear infinite;
|
|
margin: 0 auto 1rem;
|
|
}
|
|
@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="spinner"></div>
|
|
<h2>🔐 Passkey Auth</h2>
|
|
<p id="status">Checking authentication status...</p>
|
|
</div>
|
|
|
|
<script>
|
|
function updateStatus(message) {
|
|
document.getElementById('status').textContent = message;
|
|
}
|
|
|
|
function getRedirectUrl() {
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
// Check for both 'redirect' and 'rd' parameters (nginx uses 'rd' by default)
|
|
// Prefer 'redirect' over 'rd' if both are present
|
|
const redirectParam = urlParams.get('redirect');
|
|
const rdParam = urlParams.get('rd');
|
|
|
|
console.log('URL params:', {
|
|
redirect: redirectParam,
|
|
rd: rdParam,
|
|
search: window.location.search
|
|
});
|
|
|
|
return redirectParam || rdParam;
|
|
}
|
|
|
|
function redirectToTarget(url) {
|
|
try {
|
|
const decodedUrl = decodeURIComponent(url);
|
|
updateStatus(`Redirecting to ${new URL(decodedUrl).hostname}...`);
|
|
console.log('Redirecting to:', decodedUrl);
|
|
// Small delay to show the message
|
|
setTimeout(() => {
|
|
window.location.href = decodedUrl;
|
|
}, 1000);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error processing redirect URL:', error);
|
|
updateStatus('Invalid redirect URL');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function redirectToLogin() {
|
|
const redirectUrl = getRedirectUrl();
|
|
if (redirectUrl) {
|
|
// Preserve the redirect parameter when going to login
|
|
const loginUrl = `/login.html?redirect=${encodeURIComponent(redirectUrl)}`;
|
|
updateStatus('Redirecting to login...');
|
|
setTimeout(() => {
|
|
window.location.href = loginUrl;
|
|
}, 1000);
|
|
} else {
|
|
// No redirect parameter, just go to login
|
|
window.location.href = '/login.html';
|
|
}
|
|
}
|
|
|
|
async function checkAuthAndRedirect() {
|
|
const redirectUrl = getRedirectUrl();
|
|
|
|
if (!redirectUrl) {
|
|
updateStatus('No redirect URL provided');
|
|
setTimeout(() => {
|
|
window.location.href = '/login.html';
|
|
}, 2000);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
updateStatus('Checking authentication...');
|
|
|
|
const response = await fetch('/api/auth/status', {
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (response.ok) {
|
|
const userData = await response.json();
|
|
if (userData.authenticated) {
|
|
updateStatus(`Welcome back, ${userData.user.display_name}!`);
|
|
redirectToTarget(redirectUrl);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Not authenticated, redirect to login with the target URL
|
|
updateStatus('Authentication required...');
|
|
redirectToLogin();
|
|
|
|
} catch (error) {
|
|
console.error('Auth check failed:', error);
|
|
updateStatus('Authentication check failed...');
|
|
redirectToLogin();
|
|
}
|
|
}
|
|
|
|
// Start the process when page loads
|
|
document.addEventListener('DOMContentLoaded', checkAuthAndRedirect);
|
|
</script>
|
|
</body>
|
|
</html>
|