Skip to content

Commit

Permalink
Update onRedirectNavigate override, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-arroyo committed Aug 13, 2024
1 parent b588185 commit 69abcdd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
42 changes: 30 additions & 12 deletions lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,19 +584,37 @@ export class StandardController implements IController {
scenarioId: request.scenarioId,
});

// Override on request only if set, as onRedirectNavigate field is deprecated
const onRedirectNavigateCb = request.onRedirectNavigate;
request.onRedirectNavigate = (url: string) => {
const navigate =
typeof onRedirectNavigateCb === "function"
? onRedirectNavigateCb(url)
: undefined;
if (navigate !== false) {
atrMeasurement.end({ success: true });
} else {
atrMeasurement.discard();
}
return navigate;
};
if (onRedirectNavigateCb) {
request.onRedirectNavigate = (url: string) => {
const navigate =
typeof onRedirectNavigateCb === "function"
? onRedirectNavigateCb(url)
: undefined;
if (navigate !== false) {
atrMeasurement.end({ success: true });
} else {
atrMeasurement.discard();
}
return navigate;
};
} else {
const configOnRedirectNavigateCb =
this.config.auth.onRedirectNavigate;
this.config.auth.onRedirectNavigate = (url: string) => {
const navigate =
typeof configOnRedirectNavigateCb === "function"
? configOnRedirectNavigateCb(url)
: undefined;
if (navigate !== false) {
atrMeasurement.end({ success: true });
} else {
atrMeasurement.discard();
}
return navigate;
};
}

// If logged in, emit acquire token events
const isLoggedIn = this.getAllAccounts().length > 0;
Expand Down
46 changes: 46 additions & 0 deletions lib/msal-browser/test/app/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,52 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
pca.acquireTokenRedirect(loginRequest);
});

it("emits pre-redirect telemetry event when onRedirectNavigate callback is set in configuration", async () => {
const onRedirectNavigate = (url: string) => {
expect(url).toBeDefined();
};

pca = new PublicClientApplication({
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
onRedirectNavigate,
},
telemetry: {
client: new BrowserPerformanceClient(testAppConfig),
application: {
appName: TEST_CONFIG.applicationName,
appVersion: TEST_CONFIG.applicationVersion,
},
},
});
pca = (pca as any).controller;
await pca.initialize();

const callbackId = pca.addPerformanceCallback((events) => {
expect(events[0].success).toBe(true);
expect(events[0].name).toBe(
PerformanceEvents.AcquireTokenPreRedirect
);
pca.removePerformanceCallback(callbackId);
});

jest.spyOn(
NavigationClient.prototype,
"navigateExternal"
).mockImplementation(() => Promise.resolve(true));

jest.spyOn(PkceGenerator, "generatePkceCodes").mockResolvedValue({
challenge: TEST_CONFIG.TEST_CHALLENGE,
verifier: TEST_CONFIG.TEST_VERIFIER,
});
const loginRequest: RedirectRequest = {
redirectUri: TEST_URIS.TEST_REDIR_URI,
scopes: ["user.read", "openid", "profile"],
state: TEST_STATE_VALUES.USER_STATE,
};
await pca.acquireTokenRedirect(loginRequest);
});

it("discards pre-redirect telemetry event when onRedirectNavigate callback returns false", async () => {
const onRedirectNavigate = (url: string) => {
return false;
Expand Down

0 comments on commit 69abcdd

Please sign in to comment.