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-3736] Fix: Options Parameter not being passed in Hooks authorize method #542

Merged
merged 3 commits into from
Oct 25, 2022
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
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