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

feat(loginWithRedirect): add redirectMethod option #717

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
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