diff --git a/.circleci/config.yml b/.circleci/config.yml index f433b2274..8beedee80 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -125,8 +125,8 @@ jobs: - run: name: Install SPA JS command: | - pwd yarn add "../spa-js" + touch src/auth0.js echo -e "$AUTH0_CONTENT" > src/auth0.js; echo -e "$IMPORT_STATEMENT"|cat - src/main.js > /tmp/out && mv /tmp/out src/main.js; working_directory: my-app diff --git a/__tests__/Auth0Client/helpers.ts b/__tests__/Auth0Client/helpers.ts index 8446bac51..b5023bea3 100644 --- a/__tests__/Auth0Client/helpers.ts +++ b/__tests__/Auth0Client/helpers.ts @@ -137,7 +137,8 @@ export const loginWithRedirectFn = (mockWindow, mockFetch) => { customCallbackUrl } = processDefaultLoginWithRedirectOptions(testConfig); await auth0.loginWithRedirect(options); - expect(mockWindow.location.assign).toHaveBeenCalled(); + const redirectMethod = options?.redirectMethod || 'assign'; + expect(mockWindow.location[redirectMethod]).toHaveBeenCalled(); if (error && errorDescription) { window.history.pushState( diff --git a/__tests__/Auth0Client/loginWithRedirect.test.ts b/__tests__/Auth0Client/loginWithRedirect.test.ts index 2f91db86e..b3e88e912 100644 --- a/__tests__/Auth0Client/loginWithRedirect.test.ts +++ b/__tests__/Auth0Client/loginWithRedirect.test.ts @@ -69,6 +69,10 @@ describe('Auth0Client', () => { assign: { configurable: true, value: jest.fn() + }, + replace: { + configurable: true, + value: jest.fn() } } ); @@ -186,6 +190,21 @@ describe('Auth0Client', () => { }); }); + it('should log the user in by calling window.location.replace when redirectMethod=replace param is passed', async () => { + const auth0 = setup(); + + await loginWithRedirect(auth0, { + audience: 'test_audience', + redirectMethod: 'replace' + }); + + const url = new URL(mockWindow.location.replace.mock.calls[0][0]); + + assertUrlEquals(url, TEST_DOMAIN, '/authorize', { + audience: 'test_audience' + }); + }); + it('should log the user in with custom params', async () => { const auth0 = setup(); diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index b40ae782f..77352b924 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -480,8 +480,9 @@ export default class Auth0Client { * @param options */ public async loginWithRedirect(options: RedirectLoginOptions = {}) { - const url = await this.buildAuthorizeUrl(options); - window.location.assign(url); + const { redirectMethod, ...urlOptions } = options; + const url = await this.buildAuthorizeUrl(urlOptions); + window.location[redirectMethod || 'assign'](url); } /** diff --git a/src/global.ts b/src/global.ts index 66a4c3baf..867670c7e 100644 --- a/src/global.ts +++ b/src/global.ts @@ -233,6 +233,10 @@ export interface RedirectLoginOptions extends BaseLoginOptions { * Used to add to the URL fragment before redirecting */ fragment?: string; + /** + * Used to select the window.location method used to redirect + */ + redirectMethod?: 'replace' | 'assign'; } export interface RedirectLoginResult {