diff --git a/src/views/pages/auth/Callback.vue b/src/views/pages/auth/Callback.vue index 868f0ca..4f6824f 100644 --- a/src/views/pages/auth/Callback.vue +++ b/src/views/pages/auth/Callback.vue @@ -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 diff --git a/src/views/pages/auth/Login.vue b/src/views/pages/auth/Login.vue index eadee87..2b34ee4 100644 --- a/src/views/pages/auth/Login.vue +++ b/src/views/pages/auth/Login.vue @@ -31,54 +31,70 @@ 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 ks-document'); - router.push({ name: 'ks-document' }); - } - }) - .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."; @@ -114,6 +130,7 @@ const logoutAD = async () => { } }; + const login_old = async () => { console.log('[login_old] Login con username/password:', username.value); try {