From 1abedea4fd0060f0198cb9d4889ddc41118fb010 Mon Sep 17 00:00:00 2001 From: Will Hackett Date: Thu, 31 Oct 2019 09:31:29 +1100 Subject: [PATCH] feat(Fragment Param): added fragment parameter to redirect options --- __tests__/index.test.ts | 11 +++++++++++ src/Auth0Client.ts | 3 ++- src/global.ts | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 89e654e4a..7ee7139b9 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -527,6 +527,17 @@ describe('Auth0', () => { `https://test.auth0.com/authorize?query=params${TEST_TELEMETRY_QUERY_STRING}` ); }); + it('calls `window.location.assign` with the correct url and fragment if provided', async () => { + const { auth0 } = await setup(); + + await auth0.loginWithRedirect({ + ...REDIRECT_OPTIONS, + fragment: '/reset' + }); + expect(window.location.assign).toHaveBeenCalledWith( + `https://test.auth0.com/authorize?query=params${TEST_TELEMETRY_QUERY_STRING}#/reset` + ); + }); it('can be called with no arguments', async () => { const { auth0 } = await setup(); diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index 7b2bb04af..7a00589c8 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -218,6 +218,7 @@ export default class Auth0Client { const code_verifier = createRandomString(); const code_challengeBuffer = await sha256(code_verifier); const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer); + const fragment = options.fragment ? `#${options.fragment}` : ''; const params = this._getParams( authorizeOptions, stateIn, @@ -233,7 +234,7 @@ export default class Auth0Client { scope: params.scope, audience: params.audience || 'default' }); - window.location.assign(url); + window.location.assign(url + fragment); } /** diff --git a/src/global.ts b/src/global.ts index 4e4529fbf..839943848 100644 --- a/src/global.ts +++ b/src/global.ts @@ -118,6 +118,10 @@ interface RedirectLoginOptions extends BaseLoginOptions { * Used to store state before doing the redirect */ appState?: any; + /** + * Used to add to the URL fragment before redirecting + */ + fragment?: string; } interface RedirectLoginResult {