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

[SDK-2522] Check for state along with error param #231

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ describe('Auth0Provider', () => {
});

it('should handle redirect callback errors', async () => {
window.history.pushState({}, document.title, '/?error=__test_error__');
window.history.pushState(
{},
document.title,
'/?error=__test_error__&state=__test_state__'
);
clientMock.handleRedirectCallback.mockRejectedValue(
new Error('__test_error__')
);
Expand Down
13 changes: 11 additions & 2 deletions __tests__/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ describe('utils hasAuthParams', () => {
].forEach((search) => expect(hasAuthParams(search)).toBeTruthy());
});

it('should recognise the error param', async () => {
it('should recognise the error and state param', async () => {
[
'?error=1&state=2',
'?foo=1&state=2&error=3',
'?error=1&foo=2&state=3',
'?state=1&error=2&foo=3',
].forEach((search) => expect(hasAuthParams(search)).toBeTruthy());
});

it('should ignore the error param without state param', async () => {
['?error=1', '?foo=1&error=2', '?error=1&foo=2'].forEach((search) =>
expect(hasAuthParams(search)).toBeTruthy()
expect(hasAuthParams(search)).toBeFalsy()
);
});

Expand Down
2 changes: 1 addition & 1 deletion examples/cra-react-router/src/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function Nav() {

{isAuthenticated ? (
<div>
<span id="hello">Hello, {user.name}!</span>{' '}
<span id="hello">Hello, {user?.name}!</span>{' '}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixing an E2E test

<button
className="btn btn-outline-secondary"
id="logout"
Expand Down
4 changes: 2 additions & 2 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const STATE_RE = /[?&]state=[^&]+/;
const ERROR_RE = /[?&]error=[^&]+/;

export const hasAuthParams = (searchParams = window.location.search): boolean =>
(CODE_RE.test(searchParams) && STATE_RE.test(searchParams)) ||
ERROR_RE.test(searchParams);
(CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
STATE_RE.test(searchParams);

const normalizeErrorFn = (fallbackMessage: string) => (
error: Error | { error: string; error_description?: string } | ProgressEvent
Expand Down