I have a React SPA and I'm using msal to authenticate Microsoft users using loginRedirect. After the login, I'm acquiring an access token silently using acquireTokenSilent to call a web API. When acquiring the access token fails and interaction is required, I'm using acquireTokenRedirect.
When I use acquireTokenRedirect, what I see is: 1. The user is redirected to login.microsoftonline.com. 2. A 302 response is returned with Location header that contains the redirect url + the access token. 3. A GET request to my redirect url - my callback gets called. 4. Another redirect to my app root.
This last redirect makes my app to be served again and I lose the access token from the state of the app. In addition, I lost the ability to redirect the user to a specific route.
Getting the access token:
getAccessToken = async () => {
let accessTokenRequest = { scopes: [...]
};
try {
var accessTokenResponse = await
this.authAgent.acquireTokenSilent(accessTokenRequest);
return accessTokenResponse.accessToken;
} catch (error) {
const errorCode = error.name;
if (errorCode === "consent_required" || errorCode === "interaction_required") {
await this.authAgent.acquireTokenRedirect(accessTokenRequest);
}
throw error;
}
};