Repository: auth0/auth0-spa-js
Pull request #8 (2019-04-18 17:41:22)

Add option to specify a default redirect_uri option


Patch:
diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts
index a237ebd..ca3524d 100644
--- a/src/Auth0Client.ts
+++ b/src/Auth0Client.ts
@@ -63,7 +63,7 @@ export default class Auth0Client {
       response_mode: 'query',
       state,
       nonce,
-      redirect_uri,
+      redirect_uri: this.options.redirect_uri || redirect_uri,
       code_challenge,
       code_challenge_method: 'S256'
     };
diff --git a/src/global.ts b/src/global.ts
index 11d60d7..d3b6db0 100644
--- a/src/global.ts
+++ b/src/global.ts
@@ -30,6 +30,14 @@ interface Auth0ClientOptions extends BaseLoginOptions {
    * The Client ID found on your Application settings page
    */
   client_id: string;
+  /**
+   * The default URL where Auth0 will redirect your browser to with
+   * the authentication result. It must be whitelisted in
+   * the "Allowed Callback URLs" in your Auth0 Application's
+   * settings. If not provided here, should be provided in the other
+   * methods that provide authentication.
+   */
+  redirect_uri?: string;
   leeway?: number;
 }
 


Test patch:
diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts
index a4277c7..451633a 100644
--- a/__tests__/index.test.ts
+++ b/__tests__/index.test.ts
@@ -123,6 +123,25 @@ describe('Auth0', () => {
         code_challenge_method: 'S256'
       });
     });
+    it('creates correct query params when providing a default redirect_uri', async () => {
+      const redirect_uri = 'https://custom-redirect-uri/callback';
+      const { auth0, utils } = await setup({
+        redirect_uri
+      });
+
+      await auth0.loginWithPopup({});
+      expect(utils.createQueryParams).toHaveBeenCalledWith({
+        client_id: TEST_CLIENT_ID,
+        scope: TEST_SCOPES,
+        response_type: TEST_CODE,
+        response_mode: 'web_message',
+        state: TEST_ENCODED_STATE,
+        nonce: TEST_RANDOM_STRING,
+        redirect_uri,
+        code_challenge: TEST_BASE64_ENCODED_STRING,
+        code_challenge_method: 'S256'
+      });
+    });
     it('creates correct query params with custom params', async () => {
       const { auth0, utils } = await setup();
 
@@ -259,6 +278,25 @@ describe('Auth0', () => {
         code_challenge_method: 'S256'
       });
     });
+    it('creates correct query params when providing a default redirect_uri', async () => {
+      const redirect_uri = 'https://custom-redirect-uri/callback';
+      const { auth0, utils } = await setup({
+        redirect_uri
+      });
+
+      await auth0.loginWithRedirect(REDIRECT_OPTIONS);
+      expect(utils.createQueryParams).toHaveBeenCalledWith({
+        client_id: TEST_CLIENT_ID,
+        scope: TEST_SCOPES,
+        response_type: TEST_CODE,
+        response_mode: 'query',
+        state: TEST_ENCODED_STATE,
+        nonce: TEST_RANDOM_STRING,
+        redirect_uri,
+        code_challenge: TEST_BASE64_ENCODED_STRING,
+        code_challenge_method: 'S256'
+      });
+    });
     it('creates correct query params with custom params', async () => {
       const { auth0, utils } = await setup();
 
@@ -598,6 +636,26 @@ describe('Auth0', () => {
           code_challenge_method: 'S256'
         });
       });
+      it('creates correct query params when providing a default redirect_uri', async () => {
+        const redirect_uri = 'https://custom-redirect-uri/callback';
+        const { auth0, utils } = await setup({
+          redirect_uri
+        });
+
+        await auth0.getTokenSilently();
+        expect(utils.createQueryParams).toHaveBeenCalledWith({
+          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,
+          code_challenge: TEST_BASE64_ENCODED_STRING,
+          code_challenge_method: 'S256'
+        });
+      });
       it('creates correct query params with custom params', async () => {
         const { auth0, utils } = await setup();