-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About migrating MSAL 2.x to MSAL 3.x #6498
Comments
You do not need to initialize yourself if you are using msal-react. It is handled under the hood by the msal-react library, starting in msal-react v1.4.0. Continue to use the |
Hi, @tnorling Is it possible to simply have a flag indicating that the MSAL initialization is complete, since the useEffect(() => {
if (inProgress === 'none' && account) {
// do something, acquireToken
}
}, [account, inProgress]); if I also want to ask when |
My issue is similar to this one (#6500). |
Where does it say that in the docs - from my understanding, the docs tell me the opposite. The docs refer to the initialization of msal-browser (https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-react#msal-basics) and in the examples The samples helped us now, as we couldn't simply apply the msal-browser docs for initialization due to top-level-await, which is in conflict with our current browser target. The approach in the samples don't use top-level-await so can be applied easily. The docs/release notes could have been clearer on this change. |
@tnorling @jo-arroyo |
@tnorling @jo-arroyo sorry to bother, any update on this? I've just upgrated msal-browser to v3.1.0 and I'm getting the following error:
and I got my export const msalInstance = new PublicClientApplication(msalConfig)
export const setToken = (token: string) => {
// ....call API
}
msalInstance.initialize().then(() => {
// Account selection logic is app dependent. Adjust as needed for different use cases.
const accounts = msalInstance.getAllAccounts()
if (accounts.length > 0) {
msalInstance.setActiveAccount(accounts[0])
}
msalInstance.addEventCallback((event) => {
if (event?.eventType === EventType.LOGIN_SUCCESS && event?.payload) {
if ('account' in event.payload) {
const account = event?.payload?.account
if (account) {
msalInstance.setActiveAccount(account)
}
}
if ('accessToken' in event.payload) {
try {
if (!event?.payload?.accessToken) {
throw new Error('accessToken is undefined')
}
setToken(event?.payload?.accessToken)
} catch (error: any) {
logger.error(error)
}
}
}
if (event?.eventType === EventType.LOGOUT_SUCCESS) {
try {
// ....call API
} catch (error: any) {
logger.error(error)
}
}
})
}) I'm using |
As long as you are checking the inProgress state before invoking other asynchronous APIs you cannot get into this error state. You also should not be invoking token acquisition APIs outside the context of the MsalProvider so the inProgress state variable should be available everywhere you need it.
Yes
inProgress tracks operations that can't run in parallel with other operations and helps you ensure that those things are executed in serial (initialization, interaction). acquireTokenSilent does not block other APIs from executing and can be executed in parallel so does not get reflected in inProgress. When the MsalProvider component is rendered inProgress is set to "Startup", PCA.initialize will be called, then PCA.handleRedirectPromise will be called and inProgress set to "handleRedirect". When handleRedirectPromise finishes inProgress will be set to "none". From then on, you're on your own. Each time you invoke ssoSilent inProgress will be set to "ssoSilent" and when that completes, back to "None". Additionally, anytime you invoke loginRedirect/loginPopup/acquireTokenRedirect/acquireTokenPopup inProgress will be set to "acquireToken" and, when the operation completes, back to "None" |
@tnorling |
#6498 (comment) |
Core Library
MSAL.js (@azure/msal-browser)
Core Library Version
3.1.0
Wrapper Library
MSAL React (@azure/msal-react)
Wrapper Library Version
2.0.3
Public or Confidential Client?
Public
Description
Hello, according to the v-2 migration guideline, one of the breaking changes is that we must initialize the application object. We need to wait for the initialization to resolve before invoking other MSAL APIs. I find this a bit awkward. It means we have to manage a state ourselves to know if the initialization is complete, like this:
In all places where we use MSAL API, we have to use this flag to determine if we can invoke the MSAL API, such as
acquireTokenSilent
andloginRedirect
. I could manage this flag using context or Redux, but would it be better if MSAL provided a variable, like:Also, I'd like to ask if my approach is correct. Do I still need to use this flag to determine whether to render the login button? I have a feeling this isn't the best approach
MSAL Configuration
No response
Relevant Code Snippets
No response
Identity Provider
None
Source
Internal (Microsoft)
The text was updated successfully, but these errors were encountered: