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

feat: Improve popup open error #1292

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion __tests__/Auth0Client/getTokenWithPopup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as scope from '../../src/scope';
import {
assertPostFn,
fetchResponse,
setupFailingPopup,
setupFn,
setupMessageEventLister
} from './helpers';
Expand All @@ -24,7 +25,7 @@ import {
TEST_STATE
} from '../constants';

import { Auth0ClientOptions } from '../../src';
import { Auth0ClientOptions, PopupOpenError } from '../../src';
import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
import { expect } from '@jest/globals';

Expand Down Expand Up @@ -214,5 +215,12 @@ describe('Auth0Client', () => {

expect(config.popup.location.href).toMatch(/global-audience/);
});

it('should fail if the popup cannot be opened', async () => {
const auth0 = setup();
setupFailingPopup(mockWindow);

await expect(auth0.getTokenWithPopup()).rejects.toThrow(PopupOpenError);
})
});
});
6 changes: 6 additions & 0 deletions __tests__/Auth0Client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ export const setupMessageEventLister = (
});
};

export const setupFailingPopup = (
mockWindow: any
) => {
mockWindow.open.mockReturnValue(null);
}

export const loginWithPopupFn = (mockWindow, mockFetch) => {
return async (
auth0,
Expand Down
7 changes: 3 additions & 4 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
AuthenticationError,
GenericError,
MissingRefreshTokenError,
PopupOpenError,
TimeoutError
} from './errors';

Expand Down Expand Up @@ -361,9 +362,7 @@ export class Auth0Client {
config.popup = openPopup('');

if (!config.popup) {
throw new Error(
'Unable to open a popup for loginWithPopup - window.open returned `null`'
);
throw new PopupOpenError();
}
}

Expand Down Expand Up @@ -625,7 +624,7 @@ export class Auth0Client {
*
* If refresh tokens are used, the token endpoint is called directly with the
* 'refresh_token' grant. If no refresh token is available to make this call,
* the SDK will only fall back to using an iframe to the '/authorize' URL if
* the SDK will only fall back to using an iframe to the '/authorize' URL if
* the `useRefreshTokensFallback` setting has been set to `true`. By default this
* setting is `false`.
*
Expand Down
8 changes: 8 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export class PopupCancelledError extends GenericError {
}
}

export class PopupOpenError extends GenericError {
constructor() {
super('popup_open', 'Unable to open a popup for loginWithPopup - window.open returned `null`');
//https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, PopupOpenError.prototype);
}
}

/**
* Error thrown when the token exchange results in a `mfa_required` error
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
TimeoutError,
PopupTimeoutError,
PopupCancelledError,
PopupOpenError,
MfaRequiredError,
MissingRefreshTokenError
} from './errors';
Expand Down