Fix login using retry
This commit is contained in:
@@ -44,8 +44,8 @@ onMounted(async () => {
|
|||||||
localStorage.setItem('msalUser', JSON.stringify(response.account));
|
localStorage.setItem('msalUser', JSON.stringify(response.account));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait 1 second to avoid race condition
|
// Wait 5 second to avoid race condition
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||||
message.value = 'Logging in to the application...';
|
message.value = 'Logging in to the application...';
|
||||||
|
|
||||||
// Token exchange function with retry
|
// Token exchange function with retry
|
||||||
|
|||||||
@@ -31,54 +31,69 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const tryTokenExchange = async (accessToken, retry = false) => {
|
||||||
|
try {
|
||||||
|
console.log(`[loginAD] Call to /msauth/exchange (retry=${retry})...`);
|
||||||
|
const res = await axios.post(
|
||||||
|
'/msauth/exchange',
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
headers: { Authorization: `Bearer ${accessToken}` }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return res;
|
||||||
|
} catch (err) {
|
||||||
|
if (!retry) {
|
||||||
|
console.warn('[loginAD] First attempt failed, waiting 1500ms and retrying...');
|
||||||
|
await new Promise(r => setTimeout(r, 1500));
|
||||||
|
return tryTokenExchange(accessToken, true);
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loginAD = async () => {
|
const loginAD = async () => {
|
||||||
console.log('[loginAD] Avvio login AD...');
|
console.log('[loginAD] Avvio login AD...');
|
||||||
try {
|
try {
|
||||||
var token = await msalInstance.acquireTokenSilent({
|
const token = await msalInstance.acquireTokenSilent({
|
||||||
scopes: msalrequest.scopes,
|
scopes: msalrequest.scopes,
|
||||||
account: msaccount.value
|
account: msaccount.value
|
||||||
});
|
});
|
||||||
console.log('[loginAD] Token MSAL ottenuto:', token);
|
console.log('[loginAD] Token MSAL ottenuto:', token);
|
||||||
|
|
||||||
axios
|
// Inserisci una pausa per evitare race condition
|
||||||
.post(
|
await new Promise(r => setTimeout(r, 5000));
|
||||||
'/msauth/exchange',
|
|
||||||
{},
|
let exchangeResponse;
|
||||||
{
|
try {
|
||||||
headers: { Authorization: `Bearer ${token.accessToken}` }
|
exchangeResponse = await tryTokenExchange(token.accessToken);
|
||||||
}
|
console.log('[loginAD] Response from /msauth/exchange:', exchangeResponse.data);
|
||||||
)
|
|
||||||
.then((res) => {
|
if (exchangeResponse.data.token) {
|
||||||
console.log('[loginAD] Token exchange response:', res.data);
|
auth.token(null, exchangeResponse.data.token, false);
|
||||||
if (res.data.token) {
|
console.log('[loginAD] Calling auth.fetch()...');
|
||||||
auth.token(null, res.data.token, false);
|
const resp = await auth.fetch();
|
||||||
auth.fetch()
|
console.log('[loginAD] User fetch response:', resp.data.data);
|
||||||
.then((response) => {
|
|
||||||
console.log('[loginAD] User fetch response:', response.data.data);
|
const userData = resp.data.data;
|
||||||
if (!response.data.data.selectedProject) {
|
if (!userData.selectedProject) {
|
||||||
console.log('[loginAD] Nessun progetto selezionato, redirect a projects-list');
|
console.log('[loginAD] No project selected → projects-list');
|
||||||
router.push({ name: 'projects-list' });
|
router.push({ name: 'projects-list' });
|
||||||
} else {
|
|
||||||
console.log('[loginAD] Progetto selezionato, redirect a scenario-list');
|
|
||||||
router.push({ name: 'scenario-list' });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((res) => {
|
|
||||||
console.error('[loginAD] Errore su fetch user dopo token exchange:', res);
|
|
||||||
error.value = 'User not configured for this application. Contact the administrator for details.';
|
|
||||||
visible.value = true;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
error.value = 'User not configured for this application. Contact the administrator for details.';
|
console.log('[loginAD] Project selected → scenario-list');
|
||||||
visible.value = true;
|
router.push({ name: 'scenario-list' });
|
||||||
console.error('[loginAD] Nessun token ricevuto dal backend');
|
|
||||||
}
|
}
|
||||||
})
|
} else {
|
||||||
.catch((res) => {
|
console.error('[loginAD] Nessun token backend ricevuto:', exchangeResponse.data);
|
||||||
console.error('[loginAD] Errore su token exchange:', res);
|
error.value = 'User not configured for this application. Contact the administrator.';
|
||||||
error.value = 'User not configured for this application. Contact the administrator for details.';
|
|
||||||
visible.value = true;
|
visible.value = true;
|
||||||
});
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[loginAD] Errore su token exchange (dopo retry):', err.response?.data || err);
|
||||||
|
error.value = 'An error occurred during the token exchange. Please try again.';
|
||||||
|
visible.value = true;
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[loginAD] Errore generale:', e);
|
console.error('[loginAD] Errore generale:', e);
|
||||||
error.value = "Errore durante il login AD. Contattare l'amministratore.";
|
error.value = "Errore durante il login AD. Contattare l'amministratore.";
|
||||||
|
|||||||
Reference in New Issue
Block a user