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-2105] Skip redirect callback #148

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { createWrapper } from './helpers';
const clientMock = mocked(new Auth0Client({ client_id: '', domain: '' }));

describe('Auth0Provider', () => {
afterEach(() => {
window.history.pushState({}, document.title, '/');
});

it('should provide the Auth0Provider result', async () => {
const wrapper = createWrapper();
const { result, waitForNextUpdate } = renderHook(
Expand Down Expand Up @@ -186,6 +190,29 @@ describe('Auth0Provider', () => {
expect(onRedirectCallback).toHaveBeenCalledWith({ foo: 'bar' });
});

it('should skip redirect callback for non auth0 redirect callback handlers', async () => {
clientMock.isAuthenticated.mockResolvedValue(true);
window.history.pushState(
{},
document.title,
'/?code=__some_non_auth0_code__&state=__test_state__'
);
clientMock.handleRedirectCallback.mockRejectedValue(
new Error('__test_error__')
);
const wrapper = createWrapper({
skipRedirectCallback: true,
});
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(clientMock.handleRedirectCallback).not.toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(true);
expect(result.current.error).not.toBeDefined();
});

it('should login with a popup', async () => {
clientMock.getUser.mockResolvedValue(false);
const wrapper = createWrapper();
Expand Down
19 changes: 17 additions & 2 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ export interface Auth0ProviderOptions {
* See the EXAMPLES.md for more info.
*/
onRedirectCallback?: (appState: AppState) => void;
/**
* By default, if the page url has code/state params, the SDK will treat them as Auth0's and attempt to exchange the
* code for a token. In some cases the code might be for something else (another OAuth SDK perhaps). In these
* instances you can instruct the client to ignore them eg
*
* ```jsx
* <Auth0Provider
* clientId={clientId}
* domain={domain}
* skipRedirectCallback={window.location.pathname === '/stripe-oauth-callback'}
* >
* ```
*/
skipRedirectCallback?: boolean;
/**
* Your Auth0 account domain such as `'example.auth0.com'`,
* `'example.eu.auth0.com'` or , `'example.mycompany.com'`
Expand Down Expand Up @@ -184,6 +198,7 @@ const defaultOnRedirectCallback = (appState?: AppState): void => {
const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
const {
children,
skipRedirectCallback,
onRedirectCallback = defaultOnRedirectCallback,
...clientOpts
} = opts;
Expand All @@ -195,7 +210,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
useEffect(() => {
(async (): Promise<void> => {
try {
if (hasAuthParams()) {
if (hasAuthParams() && !skipRedirectCallback) {
const { appState } = await client.handleRedirectCallback();
onRedirectCallback(appState);
} else {
Expand All @@ -207,7 +222,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
dispatch({ type: 'ERROR', error: loginError(error) });
}
})();
}, [client, onRedirectCallback]);
}, [client, onRedirectCallback, skipRedirectCallback]);

const loginWithPopup = async (
options?: PopupLoginOptions,
Expand Down