Skip to content

Commit

Permalink
fix(redirect): Keep query params when using vue-router
Browse files Browse the repository at this point in the history
  • Loading branch information
jnt0r committed Apr 22, 2023
1 parent e5851ad commit de7e2b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
12 changes: 3 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,8 @@ async function initialize (app: App, authClient: Auth0Client): Promise<void> {
} finally {
const targetUrl = appState && appState.targetUrl ? appState.targetUrl : '/';

// Remove query params if vue-router is used
if (app.config.globalProperties.$router) {
const query = Object.assign({}, app.config.globalProperties.$route.query);
delete query.state;
delete query.code;
delete query.error;
delete query.error_description;
app.config.globalProperties.$router.push({ path: targetUrl, replace: true, query });
app.config.globalProperties.$router.push(targetUrl);
} else {
window.location.replace(targetUrl);
}
Expand Down Expand Up @@ -148,8 +142,8 @@ function getIdTokenClaims (): Promise<IdToken | undefined> {
return client.getIdTokenClaims();
}

function getTokenSilently(options: GetTokenSilentlyOptions & { detailedResponse: true }): Promise<GetTokenSilentlyVerboseResponse>
function getTokenSilently(options?: GetTokenSilentlyOptions): Promise<string>
function getTokenSilently(options: GetTokenSilentlyOptions & { detailedResponse: true }): Promise<GetTokenSilentlyVerboseResponse>;
function getTokenSilently(options?: GetTokenSilentlyOptions): Promise<string>;
function getTokenSilently (options?: GetTokenSilentlyOptions): Promise<undefined | string | GetTokenSilentlyVerboseResponse> {
return client.getTokenSilently(options);
}
Expand Down
10 changes: 4 additions & 6 deletions test/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ describe('initialize', () => {
Plugin.initialize(app, clientInstance).then(() => {
verify(client.handleRedirectCallback()).called();

expect(routerPush).toHaveBeenCalledWith(
{ path: '/', replace: true, query: { someOtherProperty: 'ShouldStayInState' } });
expect(routerPush).toHaveBeenCalledWith('/');
done();
});
});

test('should handle redirect and navigate using router and targetUrl', (done) => {
const clientInstance = instance(client);
setQueryValue('?code=code123&state=state456');
when(client.handleRedirectCallback()).thenResolve({ appState: { targetUrl: '/testUrl' } });
when(client.handleRedirectCallback())
.thenResolve({ appState: { targetUrl: '/testUrl?someOtherProperty=ShouldStayInState' } });

// mock vue-router
const routerPush = jest.fn();
Expand All @@ -147,7 +147,6 @@ describe('initialize', () => {
app.config.globalProperties.$route = {};
app.config.globalProperties.$router.push = routerPush;
app.config.globalProperties.$route.query = {
someOtherProperty: 'ShouldStayInState',
code: 'SomeCode',
state: 'SomeState',
error: 'SomeError',
Expand All @@ -157,8 +156,7 @@ describe('initialize', () => {
Plugin.initialize(app, clientInstance).then(() => {
verify(client.handleRedirectCallback()).called();

expect(routerPush).toHaveBeenCalledWith(
{ path: '/testUrl', replace: true, query: { someOtherProperty: 'ShouldStayInState' } });
expect(routerPush).toHaveBeenCalledWith('/testUrl?someOtherProperty=ShouldStayInState');
done();
});
});
Expand Down

0 comments on commit de7e2b2

Please sign in to comment.