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