Skip to content

Commit

Permalink
Support redirectUri again in a deprecated way
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck committed Feb 16, 2023
1 parent 525ccf8 commit 88790fc
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
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
33 changes: 31 additions & 2 deletions src/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ import { hasAuthParams, loginError, tokenError } from './utils';
import { reducer } from './reducer';
import { initialAuthState } from './auth-state';

/**
* @ignore
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function 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;
}
}

/**
* The state of the application before the user was redirected to the login page.
*/
Expand Down Expand Up @@ -93,6 +117,8 @@ declare const __VERSION__: string;
const toAuth0ClientOptions = (
opts: Auth0ProviderOptions
): Auth0ClientOptions => {
deprecateRedirectUri(opts);

return {
...opts,
auth0Client: {
Expand Down Expand Up @@ -163,8 +189,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

0 comments on commit 88790fc

Please sign in to comment.