Skip to content

Commit

Permalink
[SDK-3736] Fix: Options Parameter not being passed in Hooks `authoriz…
Browse files Browse the repository at this point in the history
…e` method (#542)

* Fix param not being passed for hooks authorize call

* Added tests to ensure options are passed along
  • Loading branch information
poovamraj authored Oct 25, 2022
1 parent 9c80510 commit 5aa3e01
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
70 changes: 54 additions & 16 deletions src/hooks/__tests__/use-auth0.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,40 @@ describe('The useAuth0 hook', () => {

await waitForNextUpdate();

expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith({
scope: 'custom-scope openid profile email',
audience: 'http://my-api',
customParam: '1234',
});
expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith(
{
scope: 'custom-scope openid profile email',
audience: 'http://my-api',
customParam: '1234',
},
{},
);
});

it('can authorize, passing through all options', async () => {
const {result, waitForNextUpdate} = renderHook(() => useAuth0(), {wrapper});

result.current.authorize(
{},
{
ephemeralSession: true,
customScheme: 'demo',
leeway: 100,
skipLegacyListener: false,
},
);

await waitForNextUpdate();

expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith(
{scope: 'openid profile email'},
{
ephemeralSession: true,
customScheme: 'demo',
leeway: 100,
skipLegacyListener: false,
},
);
});

it('adds the default scopes when none are specified', async () => {
Expand All @@ -149,9 +178,12 @@ describe('The useAuth0 hook', () => {

await waitForNextUpdate();

expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith({
scope: 'openid profile email',
});
expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith(
{
scope: 'openid profile email',
},
{},
);
});

it('adds the default scopes when some are specified with custom scope', async () => {
Expand All @@ -161,9 +193,12 @@ describe('The useAuth0 hook', () => {

await waitForNextUpdate();

expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith({
scope: 'custom-scope openid profile email',
});
expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith(
{
scope: 'custom-scope openid profile email',
},
{},
);
});

it('does not duplicate default scopes', async () => {
Expand All @@ -177,11 +212,14 @@ describe('The useAuth0 hook', () => {

await waitForNextUpdate();

expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith({
scope: 'openid profile email',
audience: 'http://my-api',
customParam: '1234',
});
expect(mockAuth0.webAuth.authorize).toHaveBeenCalledWith(
{
scope: 'openid profile email',
audience: 'http://my-api',
customParam: '1234',
},
{},
);
});

it('sets the user prop after authorizing', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/auth0-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const Auth0Provider = ({domain, clientId, children}) => {
async (...options) => {
try {
const opts = options.length ? options[0] : {};
const params = options.length > 1 ? options[1] : {};
const specifiedScopes =
opts?.scope?.split(' ').map(s => s.trim()) || [];
const scopeSet = new Set([
Expand All @@ -72,7 +73,7 @@ const Auth0Provider = ({domain, clientId, children}) => {

opts.scope = Array.from(scopeSet).join(' ');

const credentials = await client.webAuth.authorize(opts);
const credentials = await client.webAuth.authorize(opts, params);
const user = getIdTokenProfileClaims(credentials.idToken);

await client.credentialsManager.saveCredentials(credentials);
Expand Down

0 comments on commit 5aa3e01

Please sign in to comment.