bugfixes
This commit is contained in:
parent
f7b34fd953
commit
c61a15c0ce
2 changed files with 56 additions and 7 deletions
2
.env
2
.env
|
|
@ -4,4 +4,4 @@ VITE_MICROSOFT_TENANT_ID="6a51aaeb-2467-4186-9504-2a05aedc591f"
|
|||
VITE_ENTRA_CLIENT_SECRET="2iw8Q~jwqG1iacxHopBt5pstu6R45UC1gIQabcbD"
|
||||
VITE_ENTRA_AUTHORITY="https://login.microsoftonline.com/6a51aaeb-2467-4186-9504-2a05aedc591f"
|
||||
VITE_ENTRA_REDIRECT_PATH="/auth/callback/"
|
||||
VITE_ENTRA_REDIRECT_URI="https://gateway.poweron-center.net/api/msft/auth/callback/"
|
||||
VITE_ENTRA_REDIRECT_URI="https://gateway-prod.poweron-center.net/api/msft/auth/callback/"
|
||||
|
|
|
|||
|
|
@ -114,29 +114,44 @@ export function useMsalAuth() {
|
|||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const backendUrl = import.meta.env.VITE_API_BASE_URL;
|
||||
const loginUrl = `${backendUrl}/api/msft/login?state=login`;
|
||||
|
||||
console.log('🔐 Starting MSAL authentication...');
|
||||
console.log('🌐 Backend URL:', backendUrl);
|
||||
console.log('🔗 Login URL:', loginUrl);
|
||||
|
||||
// Open popup to backend Microsoft login route
|
||||
const popup = window.open(
|
||||
`${import.meta.env.VITE_API_BASE_URL}/api/msft/login?state=login`,
|
||||
loginUrl,
|
||||
'msft-login',
|
||||
'width=500,height=600,scrollbars=yes,resizable=yes'
|
||||
'width=500,height=600,scrollbars=yes,resizable=yes,top=100,left=100'
|
||||
);
|
||||
|
||||
if (!popup) {
|
||||
setMsalError('Popup was blocked. Please allow popups and try again.');
|
||||
const errorMsg = 'Popup was blocked by browser. Please allow popups for this site and try again.';
|
||||
console.error('❌ Popup blocked:', errorMsg);
|
||||
setMsalError(errorMsg);
|
||||
setIsMsalLoading(false);
|
||||
reject(new Error('Popup was blocked'));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('✅ Popup opened successfully');
|
||||
|
||||
// Listen for messages from the popup
|
||||
const messageListener = (event: MessageEvent) => {
|
||||
console.log('📨 Received message from popup:', event.origin, event.data);
|
||||
|
||||
// Verify origin for security
|
||||
const apiUrl = new URL(import.meta.env.VITE_API_BASE_URL);
|
||||
const apiUrl = new URL(backendUrl);
|
||||
if (event.origin !== apiUrl.origin) {
|
||||
console.warn('⚠️ Message from unauthorized origin:', event.origin, 'Expected:', apiUrl.origin);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.data.type === 'msft_auth_success') {
|
||||
console.log('✅ MSAL authentication successful');
|
||||
// Store the auth data with normalized field names
|
||||
if (event.data.token_data) {
|
||||
const normalizedTokenData = {
|
||||
|
|
@ -147,6 +162,7 @@ export function useMsalAuth() {
|
|||
createdAt: event.data.token_data.createdAt
|
||||
};
|
||||
localStorage.setItem('auth_data', JSON.stringify(normalizedTokenData));
|
||||
console.log('💾 Auth data stored in localStorage');
|
||||
}
|
||||
|
||||
// Clean up
|
||||
|
|
@ -166,6 +182,7 @@ export function useMsalAuth() {
|
|||
}
|
||||
});
|
||||
} else if (event.data.type === 'msft_connection_error') {
|
||||
console.error('❌ MSAL connection error:', event.data.error);
|
||||
// Handle error
|
||||
window.removeEventListener('message', messageListener);
|
||||
popup.close();
|
||||
|
|
@ -179,17 +196,49 @@ export function useMsalAuth() {
|
|||
window.addEventListener('message', messageListener);
|
||||
|
||||
// Handle popup closing without completing auth
|
||||
let popupClosedManually = false;
|
||||
const checkClosed = setInterval(() => {
|
||||
if (popup.closed) {
|
||||
clearInterval(checkClosed);
|
||||
window.removeEventListener('message', messageListener);
|
||||
setIsMsalLoading(false);
|
||||
setMsalError('Authentication was cancelled');
|
||||
reject(new Error('Authentication was cancelled'));
|
||||
|
||||
if (!popupClosedManually) {
|
||||
console.warn('⚠️ Popup was closed before authentication completed');
|
||||
setMsalError('Authentication was cancelled - popup was closed before completing login');
|
||||
} else {
|
||||
console.log('ℹ️ Popup closed after successful authentication');
|
||||
}
|
||||
|
||||
if (!popupClosedManually) {
|
||||
reject(new Error('Authentication was cancelled'));
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
// Set a timeout to detect if popup doesn't load
|
||||
const loadTimeout = setTimeout(() => {
|
||||
if (!popup.closed) {
|
||||
console.warn('⚠️ Popup did not load within 10 seconds');
|
||||
popup.close();
|
||||
clearInterval(checkClosed);
|
||||
window.removeEventListener('message', messageListener);
|
||||
setIsMsalLoading(false);
|
||||
setMsalError('Authentication timeout - please check your internet connection and try again');
|
||||
reject(new Error('Authentication timeout'));
|
||||
}
|
||||
}, 10000);
|
||||
|
||||
// Override popup.close to mark as manually closed
|
||||
const originalClose = popup.close;
|
||||
popup.close = function() {
|
||||
popupClosedManually = true;
|
||||
clearTimeout(loadTimeout);
|
||||
return originalClose.call(this);
|
||||
};
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('❌ MSAL authentication error:', error);
|
||||
setMsalError(error.message || 'Microsoft authentication failed');
|
||||
setIsMsalLoading(false);
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Reference in a new issue