-
Notifications
You must be signed in to change notification settings - Fork 948
[Auth] Fix redirect middleware #6165
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,8 @@ describe('core/auth/initializeAuth', () => { | |
async function initAndWait( | ||
persistence: Persistence | Persistence[], | ||
popupRedirectResolver?: PopupRedirectResolver, | ||
authDomain = FAKE_APP.options.authDomain | ||
authDomain = FAKE_APP.options.authDomain, | ||
blockMiddleware = false, | ||
): Promise<Auth> { | ||
const auth = new AuthImpl(FAKE_APP, FAKE_HEARTBEAT_CONTROLLER_PROVIDER, { | ||
apiKey: FAKE_APP.options.apiKey!, | ||
|
@@ -146,6 +147,12 @@ describe('core/auth/initializeAuth', () => { | |
persistence, | ||
popupRedirectResolver | ||
}); | ||
|
||
if (blockMiddleware) { | ||
auth.beforeAuthStateChanged(() => { | ||
throw new Error('blocked'); | ||
}); | ||
} | ||
// Auth initializes async. We can make sure the initialization is | ||
// flushed by awaiting a method on the queue. | ||
await auth.setPersistence(inMemoryPersistence); | ||
|
@@ -408,6 +415,84 @@ describe('core/auth/initializeAuth', () => { | |
expect(user).not.to.be.null; | ||
expect(auth.currentUser).to.eq(user); | ||
}); | ||
|
||
it('does not halt old user load if middleware throws', async () => { | ||
const stub = sinon.stub( | ||
_getInstance<PersistenceInternal>(inMemoryPersistence) | ||
); | ||
const oldUser = testUser(oldAuth, 'old-uid'); | ||
stub._get.returns(Promise.resolve(oldUser.toJSON())); | ||
const overrideSpy = sinon.spy(_getInstance<PopupRedirectResolverInternal>(browserPopupRedirectResolver), '_overrideRedirectResult'); | ||
const auth = await initAndWait( | ||
[inMemoryPersistence], | ||
browserPopupRedirectResolver, | ||
FAKE_APP.options.authDomain, | ||
/* blockMiddleware */ true | ||
); | ||
|
||
expect(auth.currentUser!.uid).to.eq(oldUser.uid); | ||
expect(reload._reloadWithoutSaving).to.have.been.called; | ||
expect(overrideSpy).not.to.have.been.called; | ||
}); | ||
|
||
it('Reloads and uses old user if middleware throws', async () => { | ||
const stub = sinon.stub( | ||
_getInstance<PersistenceInternal>(inMemoryPersistence) | ||
); | ||
const oldUser = testUser(oldAuth, 'old-uid'); | ||
stub._get.returns(Promise.resolve(oldUser.toJSON())); | ||
const overrideSpy = sinon.spy(_getInstance<PopupRedirectResolverInternal>(browserPopupRedirectResolver), '_overrideRedirectResult'); | ||
|
||
let user: UserInternal | null = null; | ||
completeRedirectFnStub.callsFake((auth: AuthInternal) => { | ||
user = testUser(auth, 'uid', '[email protected]'); | ||
return Promise.resolve( | ||
new UserCredentialImpl({ | ||
operationType: OperationType.SIGN_IN, | ||
user, | ||
providerId: null | ||
}) | ||
); | ||
}); | ||
|
||
const auth = await initAndWait( | ||
[inMemoryPersistence], | ||
browserPopupRedirectResolver, | ||
FAKE_APP.options.authDomain, | ||
/* blockMiddleware */ true | ||
); | ||
expect(user).not.to.be.null; | ||
expect(auth.currentUser!.uid).to.eq(oldUser.uid); | ||
expect(reload._reloadWithoutSaving).to.have.been.called; | ||
expect(overrideSpy).to.have.been.called; | ||
}); | ||
|
||
it('Nulls current user if redirect blocked by middleware', async () => { | ||
const stub = sinon.stub( | ||
_getInstance<PersistenceInternal>(inMemoryPersistence) | ||
); | ||
stub._get.returns(Promise.resolve(null)); | ||
completeRedirectFnStub.callsFake((auth: AuthInternal) => { | ||
const user = testUser(auth, 'uid', '[email protected]'); | ||
return Promise.resolve( | ||
new UserCredentialImpl({ | ||
operationType: OperationType.SIGN_IN, | ||
user, | ||
providerId: null | ||
}) | ||
); | ||
}); | ||
|
||
const auth = await initAndWait( | ||
[inMemoryPersistence], | ||
browserPopupRedirectResolver, | ||
FAKE_APP.options.authDomain, | ||
/* blockMiddleware */ true | ||
); | ||
expect(completeRedirectFnStub).to.have.been.called; | ||
expect(auth.currentUser).to.be.null; | ||
expect(reload._reloadWithoutSaving).not.to.have.been.called; | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.