170 lines
6.8 KiB
HTML
170 lines
6.8 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</title>
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/pocketbase@0.26.5/dist/pocketbase.umd.js"></script>
|
|
<script src="auth.js"></script>
|
|
</head>
|
|
<body class="font-sans bg-gradient-to-br from-indigo-500 to-purple-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">Welcome</h1>
|
|
<p class="text-gray-600 mb-8">Sign in with your Microsoft account</p>
|
|
|
|
<button id="loginBtn" onclick="login()" class="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 disabled:cursor-not-allowed">Sign in with Microsoft</button>
|
|
|
|
<div class="text-red-600 mt-4 hidden" id="error"></div>
|
|
|
|
<div class="hidden mt-8 text-left bg-gray-50 p-6 rounded-lg" id="userInfo">
|
|
<h2 class="text-lg mb-4 text-gray-800">Signed in</h2>
|
|
<p class="mb-3 text-gray-600 break-all"><strong class="text-gray-800">Name:</strong> <span id="displayName"></span></p>
|
|
<p class="mb-3 text-gray-600 break-all"><strong class="text-gray-800">Email:</strong> <span id="email"></span></p>
|
|
|
|
<button id="continueBtn" onclick="goToIndex()" class="hidden mt-4 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 disabled:cursor-not-allowed w-full">Continue to Job Info</button>
|
|
<button onclick="logout()" class="bg-gray-600 text-white border-none py-3.5 px-8 text-base rounded-md cursor-pointer transition-colors duration-200 font-medium hover:bg-gray-700 mt-4">Sign out</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const { pb, getDisplayName } = window.Auth;
|
|
|
|
const GRAPH_TOKEN_KEY = 'graphAccessToken';
|
|
const extractGraphToken = (authData) => {
|
|
return (
|
|
authData?.meta?.graphAccessToken ||
|
|
authData?.meta?.graph_token ||
|
|
authData?.meta?.graphToken ||
|
|
authData?.meta?.accessToken ||
|
|
authData?.meta?.access_token ||
|
|
authData?.meta?.token ||
|
|
authData?.meta?.rawToken ||
|
|
authData?.meta?.authData?.access_token ||
|
|
''
|
|
);
|
|
};
|
|
|
|
const loginBtn = document.getElementById('loginBtn');
|
|
const errorEl = document.getElementById('error');
|
|
const userInfo = document.getElementById('userInfo');
|
|
const continueBtn = document.getElementById('continueBtn');
|
|
const GRAPH_REAUTH_FLAG = 'graphReauthAttempted';
|
|
|
|
function goToIndex() {
|
|
window.location.href = 'index.html';
|
|
}
|
|
|
|
function showAuthedUI(displayName, email) {
|
|
loginBtn.classList.add('hidden');
|
|
loginBtn.disabled = false;
|
|
loginBtn.textContent = 'Sign in with Microsoft';
|
|
userInfo.classList.remove('hidden');
|
|
continueBtn.classList.remove('hidden');
|
|
document.getElementById('displayName').textContent = displayName || 'Unknown';
|
|
document.getElementById('email').textContent = email || 'Not available';
|
|
}
|
|
|
|
async function login() {
|
|
loginBtn.disabled = true;
|
|
loginBtn.textContent = 'Signing in...';
|
|
errorEl.classList.add('hidden');
|
|
|
|
try {
|
|
const authData = await pb.collection('users').authWithOAuth2({
|
|
provider: 'microsoft',
|
|
});
|
|
displayUserInfo(authData);
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
errorEl.textContent = error.message || 'Authentication failed. Please try again.';
|
|
errorEl.classList.remove('hidden');
|
|
loginBtn.disabled = false;
|
|
loginBtn.textContent = 'Sign in with Microsoft';
|
|
}
|
|
}
|
|
|
|
function logout() {
|
|
pb.authStore.clear();
|
|
localStorage.removeItem(GRAPH_TOKEN_KEY);
|
|
loginBtn.classList.remove('hidden');
|
|
userInfo.classList.add('hidden');
|
|
continueBtn.classList.add('hidden');
|
|
loginBtn.disabled = false;
|
|
loginBtn.textContent = 'Sign in with Microsoft';
|
|
}
|
|
|
|
function displayUserInfo(authData) {
|
|
console.log('Auth response:', authData);
|
|
console.log('Auth meta:', authData?.meta);
|
|
const displayName = getDisplayName();
|
|
const email = authData?.meta?.rawUser?.email || authData?.record?.email || pb.authStore.model?.email || 'Not available';
|
|
const graphToken = extractGraphToken(authData);
|
|
console.log('Extracted Graph token:', graphToken ? graphToken.substring(0, 30) + '...' : 'NOT FOUND');
|
|
console.log('Full meta object:', JSON.stringify(authData?.meta, null, 2));
|
|
|
|
if (graphToken) {
|
|
localStorage.setItem(GRAPH_TOKEN_KEY, graphToken);
|
|
localStorage.removeItem(GRAPH_REAUTH_FLAG);
|
|
console.log('✓ Graph token stored');
|
|
} else {
|
|
console.error('❌ Graph token NOT found in auth response!');
|
|
console.error('Available meta keys:', Object.keys(authData?.meta || {}));
|
|
// If token not in response, try fetching it from the backend
|
|
console.log('Attempting to fetch token from backend...');
|
|
fetchTokenFromBackend().then(token => {
|
|
if (token) {
|
|
localStorage.setItem(GRAPH_TOKEN_KEY, token);
|
|
console.log('✓ Graph token fetched from backend');
|
|
} else {
|
|
console.warn('⚠ Graph token could not be obtained from backend');
|
|
}
|
|
showAuthedUI(displayName, email);
|
|
}).catch(err => {
|
|
console.error('Failed to fetch token from backend:', err);
|
|
showAuthedUI(displayName, email);
|
|
});
|
|
return;
|
|
}
|
|
showAuthedUI(displayName, email);
|
|
}
|
|
|
|
async function fetchTokenFromBackend() {
|
|
try {
|
|
const response = await fetch('/api/graph-token', {
|
|
headers: {
|
|
'Authorization': `Bearer ${pb.authStore.token}`
|
|
}
|
|
});
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
return data.token || '';
|
|
}
|
|
} catch (err) {
|
|
console.warn('Token fetch error:', err.message);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
(async () => {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const reauth = params.get('reauth') === '1';
|
|
if (pb.authStore.isValid) {
|
|
try {
|
|
const authData = await pb.collection('users').authRefresh();
|
|
displayUserInfo(authData);
|
|
// If we came from a reauth flow and now have a token, go back automatically
|
|
const hasToken = localStorage.getItem(GRAPH_TOKEN_KEY);
|
|
if (reauth && hasToken) {
|
|
window.location.href = 'index.html';
|
|
}
|
|
} catch (err) {
|
|
pb.authStore.clear();
|
|
logout();
|
|
}
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|