88 lines
3.2 KiB
HTML
88 lines
3.2 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Sign In - Mode2Test</title>
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/pocketbase@0.26.5/dist/pocketbase.umd.js"></script>
|
|
</head>
|
|
<body class="font-sans bg-gradient-to-br from-blue-500 to-indigo-600 min-h-screen flex items-center justify-center">
|
|
<div class="bg-white p-12 rounded-2xl shadow-2xl text-center max-w-md w-[90%]">
|
|
<h1 class="text-gray-800 mb-2 text-3xl font-bold">Mode2Test</h1>
|
|
<p class="text-gray-600 mb-8">Sign in with your Microsoft account</p>
|
|
|
|
<button id="loginBtn" onclick="handleLogin()" class="w-full bg-blue-600 text-white border-none py-3.5 px-8 text-base rounded-md cursor-pointer transition-colors duration-200 font-medium hover:bg-blue-700 disabled:bg-gray-400">
|
|
Sign in with Microsoft
|
|
</button>
|
|
|
|
<div id="error" class="text-red-600 mt-4 hidden"></div>
|
|
</div>
|
|
|
|
<script>
|
|
const pb = new PocketBase('https://pocketbase.ccllc.pro');
|
|
|
|
async function handleLogin() {
|
|
const btn = document.getElementById('loginBtn');
|
|
btn.disabled = true;
|
|
btn.textContent = 'Signing in...';
|
|
document.getElementById('error').classList.add('hidden');
|
|
|
|
try {
|
|
const authData = await pb.collection('users').authWithOAuth2({ provider: 'microsoft' });
|
|
|
|
// Log what we got for debugging
|
|
console.log('[Auth] OAuth response:', authData);
|
|
console.log('[Auth] Meta:', authData?.meta);
|
|
|
|
// Try to extract graph token from various possible locations
|
|
const graphToken =
|
|
authData?.meta?.accessToken ||
|
|
authData?.meta?.access_token ||
|
|
authData?.meta?.graphAccessToken ||
|
|
authData?.meta?.graph_token ||
|
|
authData?.meta?.graphToken ||
|
|
authData?.meta?.rawUser?.access_token ||
|
|
'';
|
|
|
|
console.log('[Auth] Graph token found:', !!graphToken);
|
|
|
|
// Send to backend to store
|
|
const saveResponse = await fetch('/api/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ pbToken: authData.token, graphToken: graphToken || undefined })
|
|
});
|
|
|
|
if (!saveResponse.ok) {
|
|
throw new Error('Failed to save tokens to backend');
|
|
}
|
|
|
|
console.log('[Auth] Tokens saved to backend, redirecting to home');
|
|
|
|
// Clear PocketBase local storage to avoid conflicts
|
|
pb.authStore.clear();
|
|
|
|
// Redirect to home page
|
|
window.location.href = '/home.html';
|
|
} catch (err) {
|
|
document.getElementById('error').textContent = err.message || 'Login failed';
|
|
document.getElementById('error').classList.remove('hidden');
|
|
btn.disabled = false;
|
|
btn.textContent = 'Sign in with Microsoft';
|
|
}
|
|
}
|
|
|
|
// Check if already logged in via backend only
|
|
fetch('/api/auth/status')
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.authenticated) {
|
|
window.location.href = '/home.html';
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
</script>
|
|
</body>
|
|
</html>
|