Mode2Test AuthAndToken component complete: login/logout working, token persistence, background refresh active, tested and verified

This commit is contained in:
2026-01-17 15:31:44 +00:00
parent 319289cfac
commit eed8552282
13 changed files with 626 additions and 3 deletions
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>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-gray-50 min-h-screen">
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto px-4 py-4 flex justify-between items-center">
<div>
<h1 class="text-2xl font-bold text-gray-800">Mode2Test</h1>
<p class="text-sm text-gray-600">Authenticated Application</p>
</div>
<div class="flex items-center gap-4">
<div id="userDisplay" class="text-right">
<p id="userName" class="text-sm font-medium text-gray-800"></p>
<p id="userEmail" class="text-xs text-gray-600"></p>
</div>
<button onclick="handleLogout()" class="px-4 py-2 bg-red-600 text-white rounded-md hover:bg-red-700">
Logout
</button>
</div>
</div>
</header>
<main class="max-w-7xl mx-auto px-4 py-8">
<div class="bg-white p-8 rounded-lg shadow">
<h2 class="text-xl font-bold mb-4 text-gray-800">Welcome to Mode2Test</h2>
<p class="text-gray-600 mb-6">Authenticated and ready to work.</p>
<div class="bg-gray-50 p-4 rounded border border-gray-200">
<h3 class="font-medium mb-2 text-gray-800">Token Status</h3>
<div id="tokenStatus" class="text-sm text-gray-600 space-y-2">
<p>Loading...</p>
</div>
</div>
</div>
</main>
<script>
const pb = new PocketBase('https://pocketbase.ccllc.pro');
async function checkAuth() {
if (!pb.authStore.isValid) {
// Try to refresh
try {
await pb.collection('users').authRefresh();
} catch {
window.location.href = '/signin.html';
return;
}
}
// Logged in - show app
document.getElementById('userName').textContent = pb.authStore.model?.display_name || pb.authStore.model?.email || 'User';
document.getElementById('userEmail').textContent = pb.authStore.model?.email || '';
const response = await fetch('/api/auth/tokens');
if (response.ok) {
const tokens = await response.json();
const pbValid = tokens.pbToken ? '✓ Valid' : '✗ Missing';
const graphValid = tokens.graphToken ? '✓ Valid' : '✗ Not acquired';
document.getElementById('tokenStatus').innerHTML = `
<div><p><strong>PocketBase Token:</strong> ${pbValid}</p></div>
<div><p><strong>Graph Token:</strong> ${graphValid}</p></div>
`;
}
}
function handleLogout() {
if (confirm('Logout?')) {
pb.authStore.clear();
fetch('/api/auth/logout', { method: 'POST' });
window.location.href = '/signin.html';
}
}
checkAuth();
</script>
</body>
</html>
@@ -0,0 +1,73 @@
<!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
await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ pbToken: authData.token, graphToken: graphToken || undefined })
});
// Redirect to app immediately
window.location.href = '/index.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
if (pb.authStore.isValid) {
window.location.href = '/index.html';
}
</script>
</body>
</html>