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

Support redirectUri again in a deprecated way #507

Merged
merged 2 commits into from
Feb 21, 2023
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
84 changes: 84 additions & 0 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,52 @@ describe('Auth0Provider', () => {
await waitForNextUpdate();
});

it('should support redirectUri', async () => {
const opts = {
clientId: 'foo',
domain: 'bar',
redirectUri: 'baz',
};
const wrapper = createWrapper(opts);
const { waitForNextUpdate } = renderHook(() => useContext(Auth0Context), {
wrapper,
});
expect(Auth0Client).toHaveBeenCalledWith(
expect.objectContaining({
clientId: 'foo',
domain: 'bar',
authorizationParams: {
redirect_uri: 'baz',
},
})
);
await waitForNextUpdate();
});

it('should support authorizationParams.redirectUri', async () => {
const opts = {
clientId: 'foo',
domain: 'bar',
authorizationParams: {
redirectUri: 'baz',
},
};
const wrapper = createWrapper(opts);
const { waitForNextUpdate } = renderHook(() => useContext(Auth0Context), {
wrapper,
});
expect(Auth0Client).toHaveBeenCalledWith(
expect.objectContaining({
clientId: 'foo',
domain: 'bar',
authorizationParams: {
redirect_uri: 'baz',
},
})
);
await waitForNextUpdate();
});

it('should pass user agent to Auth0Client', async () => {
const opts = {
clientId: 'foo',
Expand Down Expand Up @@ -300,6 +346,44 @@ describe('Auth0Provider', () => {
});
});

it('should provide a login method supporting redirectUri', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.loginWithRedirect).toBeInstanceOf(Function);
await result.current.loginWithRedirect({
redirectUri: '__redirect_uri__',
} as never);
expect(clientMock.loginWithRedirect).toHaveBeenCalledWith({
authorizationParams: {
redirect_uri: '__redirect_uri__',
},
});
});

it('should provide a login method supporting authorizationParams.redirectUri', async () => {
const wrapper = createWrapper();
const { waitForNextUpdate, result } = renderHook(
() => useContext(Auth0Context),
{ wrapper }
);
await waitForNextUpdate();
expect(result.current.loginWithRedirect).toBeInstanceOf(Function);
await result.current.loginWithRedirect({
authorizationParams: {
redirectUri: '__redirect_uri__',
},
});
expect(clientMock.loginWithRedirect).toHaveBeenCalledWith({
authorizationParams: {
redirect_uri: '__redirect_uri__',
},
});
});

it('should provide a logout method', async () => {
const user = { name: '__test_user__' };
clientMock.getUser.mockResolvedValue(user);
Expand Down
16 changes: 13 additions & 3 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import Auth0Context, {
LogoutOptions,
RedirectLoginOptions,
} from './auth0-context';
import { hasAuthParams, loginError, tokenError } from './utils';
import {
hasAuthParams,
loginError,
tokenError,
deprecateRedirectUri,
} from './utils';
import { reducer } from './reducer';
import { initialAuthState } from './auth-state';

Expand Down Expand Up @@ -93,6 +98,8 @@ declare const __VERSION__: string;
const toAuth0ClientOptions = (
opts: Auth0ProviderOptions
): Auth0ClientOptions => {
deprecateRedirectUri(opts);

return {
...opts,
auth0Client: {
Expand Down Expand Up @@ -163,8 +170,11 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
}, [client, onRedirectCallback, skipRedirectCallback]);

const loginWithRedirect = useCallback(
(opts?: RedirectLoginOptions): Promise<void> =>
client.loginWithRedirect(opts),
(opts?: RedirectLoginOptions): Promise<void> => {
deprecateRedirectUri(opts);

return client.loginWithRedirect(opts);
},
[client]
);

Expand Down
50 changes: 39 additions & 11 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,46 @@ export const hasAuthParams = (searchParams = window.location.search): boolean =>
(CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
STATE_RE.test(searchParams);

const normalizeErrorFn = (fallbackMessage: string) => (
error: Error | { error: string; error_description?: string } | ProgressEvent
): Error => {
if ('error' in error) {
return new OAuthError(error.error, error.error_description);
}
if (error instanceof Error) {
return error;
}
return new Error(fallbackMessage);
};
const normalizeErrorFn =
(fallbackMessage: string) =>
(
error: Error | { error: string; error_description?: string } | ProgressEvent
): Error => {
if ('error' in error) {
return new OAuthError(error.error, error.error_description);
}
if (error instanceof Error) {
return error;
}
return new Error(fallbackMessage);
};

export const loginError = normalizeErrorFn('Login failed');

export const tokenError = normalizeErrorFn('Get access token failed');

/**
* @ignore
* Helper function to map the v1 `redirectUri` option to the v2 `authorizationParams.redirect_uri`
* and log a warning.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const deprecateRedirectUri = (options?: any) => {
if (options?.redirectUri) {
console.warn(
'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version'
);
options.authorizationParams = options.authorizationParams || {};
options.authorizationParams.redirect_uri = options.redirectUri;
delete options.redirectUri;
}

if (options?.authorizationParams?.redirectUri) {
console.warn(
'Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version'
);
options.authorizationParams.redirect_uri =
options.authorizationParams.redirectUri;
delete options.authorizationParams.redirectUri;
}
};