Repository: auth0/auth0-spa-js
Pull request #318 (2019-12-19 08:54:34)

Use user provided params on silent login
### Description

This PR adds support for custom query parameters in `getTokenSilently` similar to how `loginWithRedirect` does this.

### References

- [Issue on Github](https://github.com/auth0/auth0-spa-js/issues/317)

### Testing

Add a custom property to the `getTokenSilently` method call and inspect the generated url.
```js
auth0.getTokenSilently({
    scope,
    audience,
    foo: 'bar',
});
```

I was looking at integration tests I noticed that `getTokenSilently > Builds URL correctly` test is skipped. Can someone provide some context on why is that? Should the test be updated to reflect custom parameters as well?

### Checklist

- [x] All active GitHub checks for tests, formatting, and security are passing



stevehobbsdev: @nkete Thanks for raising this. Before we bring it in, let me get a look at that integration test which is being skipped and get back to you. You're right, we'll want to update that with your changes if we can.
stevehobbsdev: @nkete I'm doing some other work on this SDK and in there I've ended up removing the integration test that is being skipped. It was being skipped because it just doesn't work, so I've removed it and taken a different approach. However, it's only available in another branch so I would ignore this test for now.

However, we would require some test coverage around this new functionality you've provided - could you please add those tests to the PR?
nkete: @stevehobbsdev sure, I'll get back to you once I add the tests.
stevehobbsdev: Thanks @nkete. To clarify above, when I mentioned "test coverage" I was referring to unit tests. I'd think they'd be enough for this particular case.
nkete: @stevehobbsdev I added a test for custom params to the `getTokenSilently` section, please take a look if that'll do. Also rebased against latest master.

Patch:
diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts
index 1452177..9ad33b3 100644
--- a/src/Auth0Client.ts
+++ b/src/Auth0Client.ts
@@ -332,10 +332,17 @@ export default class Auth0Client {
     options.scope = getUniqueScopes(this.DEFAULT_SCOPE, options.scope);
 
     try {
-      if (!options.ignoreCache) {
+      const {
+        audience,
+        scope,
+        ignoreCache,
+        ...additionalQueryParams
+      } = options;
+
+      if (!ignoreCache) {
         const cache = this.cache.get({
-          scope: options.scope,
-          audience: options.audience || 'default'
+          scope,
+          audience: audience || 'default'
         });
 
         if (cache) {
@@ -352,8 +359,9 @@ export default class Auth0Client {
       const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer);
 
       const authorizeOptions = {
-        audience: options.audience,
-        scope: options.scope
+        audience,
+        scope,
+        ...additionalQueryParams
       };
 
       const params = this._getParams(


Test patch:
diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts
index d9f2ebd..0fc1aa0 100644
--- a/__tests__/index.test.ts
+++ b/__tests__/index.test.ts
@@ -1208,6 +1208,29 @@ describe('Auth0', () => {
           defaultOptionsIgnoreCacheTrue.scope
         );
       });
+      it('creates correct query params when providing user specified custom query params', async () => {
+        const { auth0, utils } = await setup();
+
+        const customQueryParameterOptions = {
+          ...defaultOptionsIgnoreCacheTrue,
+          foo: 'bar'
+        };
+        await auth0.getTokenSilently(customQueryParameterOptions);
+        expect(utils.createQueryParams).toHaveBeenCalledWith({
+          audience: defaultOptionsIgnoreCacheTrue.audience,
+          client_id: TEST_CLIENT_ID,
+          scope: TEST_SCOPES,
+          response_type: TEST_CODE,
+          response_mode: 'web_message',
+          prompt: 'none',
+          state: TEST_ENCODED_STATE,
+          nonce: TEST_RANDOM_STRING,
+          redirect_uri: 'http://localhost',
+          code_challenge: TEST_BASE64_ENCODED_STRING,
+          code_challenge_method: 'S256',
+          foo: 'bar'
+        });
+      });
       it('opens iframe with correct urls', async () => {
         const { auth0, utils } = await setup();
         await auth0.getTokenSilently(defaultOptionsIgnoreCacheTrue);