Skip to content
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

Memoize handleRedirectPromise #3072

Merged
merged 9 commits into from Feb 25, 2021
Merged

Memoize handleRedirectPromise #3072

merged 9 commits into from Feb 25, 2021

Conversation

tnorling
Copy link
Collaborator

@tnorling tnorling commented Feb 23, 2021

Multiple concurrent calls to handleRedirectPromise can cause race conditions and confusing errors when the first invocation clears temporary cache before the 2nd can read it. The official recommendation up until now has been "dont call handleRedirectPromise more than 1 time per page load" but this can be confusing and easy to overlook when using frameworks (react, angular, etc.) that may re-render components and call handleRedirectPromise again unexpectedly.

This PR aims to solve this once and for all by having all invocations of handleRedirectPromise on a single PublicClientApplication instance return the same promise and ensure that the hash processing logic is only run once.

Note: This does not solve scenarios where your page has multiple instances of PublicClientApplication calling handleRedirectPromise or where PublicClientApplication is re-instantiated and and calls handleRedirectPromise again (your app should still only instantiate PublicClientApplication once and should not be re-instantiated as a result of a re-render)

@github-actions github-actions bot added the msal-browser Related to msal-browser package label Feb 23, 2021
@@ -54,6 +54,9 @@ export abstract class ClientApplication {
// Callback for subscribing to events
private eventCallbacks: Map<string, EventCallbackFunction>;

// Redirect Response Object
private redirectResponse: Promise<AuthenticationResult | null> | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the page is re-rendered won't this variable be re-rendered also? Doesn't this only solve handleRedirectPromise being called multiple times in a single page load?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. More specifically this solves handleRedirectPromise being called multiple times per pca instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the PR description it says that it may solve issues when frameworks re-render components, we should update this in case someone gets confused thinking this will solve re-rendered instances.

Copy link
Member

@sameerag sameerag Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a min, I thought the same. :-D. Also @tnorling I do not see page refresh mentioned (or re-rendering) in the note in the description.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Page refresh is not a broken scenario. This is only to guard against concurrent calls to handleRedirectPromise throwing errors

@coveralls
Copy link

coveralls commented Feb 24, 2021

Coverage Status

Coverage increased (+2.05%) to 84.786% when pulling 2ca0228 on memoize-handleRedirectPromise into d5509a0 on dev.

Copy link
Contributor

@jasonnutter jasonnutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple comments otherwise looks good!

@tnorling tnorling added this to the @azure/msal-browser@2.12.0 milestone Feb 24, 2021
Copy link
Contributor

@pkanher617 pkanher617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 minor comment, otherwise looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
msal-browser Related to msal-browser package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants