-
Notifications
You must be signed in to change notification settings - Fork 460
chore(clerk-js): Improve Token.create to handle network failures errors #3035
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| --- | ||
|
|
||
| Update token refresh mechanism to handle network failures without raising an error |
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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { createFapiClient } from '../fapiClient'; | ||
| import { mockDevClerkInstance, mockFetch, mockNetworkFailedFetch } from '../test/fixtures'; | ||
| import { BaseResource } from './internal'; | ||
| import { Token } from './Token'; | ||
|
|
||
| describe('Token', () => { | ||
| describe('create', () => { | ||
| afterEach(() => { | ||
| // @ts-ignore | ||
| global.fetch?.mockClear(); | ||
| BaseResource.clerk = null as any; | ||
| }); | ||
|
|
||
| it('with http 500 throws error', async () => { | ||
| mockFetch(false, 500); | ||
| BaseResource.clerk = { getFapiClient: () => createFapiClient(mockDevClerkInstance) } as any; | ||
|
|
||
| await expect(Token.create('/path/to/tokens')).rejects.toMatchObject({ | ||
| message: '500', | ||
| }); | ||
|
|
||
| expect(global.fetch).toHaveBeenCalledWith( | ||
| 'https://clerk.example.com/v1/path/to/tokens?_clerk_js_version=test-0.0.0', | ||
| // TODO(dimkl): omit extra params from fetch request (eg path, url) - remove expect.objectContaining | ||
| expect.objectContaining({ | ||
| method: 'POST', | ||
| body: '', | ||
| credentials: 'include', | ||
| headers: new Headers(), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| describe('with offline browser and network failure', () => { | ||
| let warnSpy; | ||
|
|
||
| beforeEach(() => { | ||
| Object.defineProperty(window.navigator, 'onLine', { | ||
| writable: true, | ||
| value: false, | ||
| }); | ||
| warnSpy = jest.spyOn(console, 'warn').mockReturnValue(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(window.navigator, 'onLine', { | ||
| writable: true, | ||
| value: true, | ||
| }); | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('create returns empty raw string', async () => { | ||
| mockNetworkFailedFetch(); | ||
| BaseResource.clerk = { getFapiClient: () => createFapiClient(mockDevClerkInstance) } as any; | ||
|
|
||
| const token = await Token.create('/path/to/tokens'); | ||
|
|
||
| expect(global.fetch).toHaveBeenCalledWith( | ||
| 'https://clerk.example.com/v1/path/to/tokens?_clerk_js_version=test-0.0.0', | ||
| // TODO(dimkl): omit extra params from fetch request (eg path, url) - remove expect.objectContaining | ||
| expect.objectContaining({ | ||
| method: 'POST', | ||
| body: '', | ||
| credentials: 'include', | ||
| headers: new Headers(), | ||
| }), | ||
| ); | ||
|
|
||
| expect(token.getRawString()).toEqual(''); | ||
| expect(warnSpy).toBeCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with online browser and network failure', () => { | ||
| it('throws error', async () => { | ||
| mockNetworkFailedFetch(); | ||
| BaseResource.clerk = { getFapiClient: () => createFapiClient(mockDevClerkInstance) } as any; | ||
|
|
||
| await expect(Token.create('/path/to/tokens')).rejects.toMatchObject({ | ||
| message: | ||
| 'ClerkJS: Network error at "https://clerk.example.com/v1/path/to/tokens?_clerk_js_version=test-0.0.0" - TypeError: Failed to fetch. Please try again.', | ||
| }); | ||
|
|
||
| expect(global.fetch).toHaveBeenCalledWith( | ||
| 'https://clerk.example.com/v1/path/to/tokens?_clerk_js_version=test-0.0.0', | ||
| // TODO(dimkl): omit extra params from fetch request (eg path, url) - remove expect.objectContaining | ||
| expect.objectContaining({ | ||
| method: 'POST', | ||
| body: '', | ||
| credentials: 'include', | ||
| headers: new Headers(), | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this issue caused by an error thrown inside the
tokenResolverpromise?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change attempts to fix the case where the network is offline causing the
Token.createto return anewToken : null. Currently an error related toCannot read properties of null (reading 'jwt')is thrown due to code in the following lines. To mitigate that (with the code above) I've added this guard to delete the key (if it exists) and return since there is no cookie to set.Does this make any sense?