Skip to content

Commit

Permalink
feat(loginWithRedirect): add redirectMethod option (#717)
Browse files Browse the repository at this point in the history
* feat(loginWithRedirect): add redirectMethod option

* Add touch command which might fix Vue build errors

Co-authored-by: Steve Hobbs <steve.hobbs@auth0.com>
Co-authored-by: Steve Hobbs <steve.hobbs.mail@gmail.com>
  • Loading branch information
3 people authored Mar 22, 2021
1 parent b78ca01 commit c2dd91e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion __tests__/Auth0Client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions __tests__/Auth0Client/loginWithRedirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe('Auth0Client', () => {
assign: {
configurable: true,
value: jest.fn()
},
replace: {
configurable: true,
value: jest.fn()
}
}
);
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c2dd91e

Please sign in to comment.