Skip to content

New resetInProgressEvent method to reset event to None after handleRedirectPromise completes #7682

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

Merged
merged 11 commits into from
Apr 4, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "added resetInProgressEvent function to reset event after handleRedirectPromise is called #7682",
"packageName": "@azure/msal-angular",
"email": "[email protected]",
"dependentChangeType": "patch"
}
3 changes: 2 additions & 1 deletion lib/msal-angular/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = function (config) {
jasmine: {
failSpecWithNoExpectations: true
},
clearContext: false // leave Jasmine Spec Runner output visible in browser
clearContext: false, // leave Jasmine Spec Runner output visible in browser
captureConsole: true // turns on console logging for test debugging
},
coverageIstanbulReporter: {
dir: require("path").join(__dirname, "./coverage"),
Expand Down
26 changes: 26 additions & 0 deletions lib/msal-angular/src/msal.broadcast.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function initializeMsal(providers: any[] = []) {
}),
],
providers: [MsalBroadcastService, ...providers],
teardown: { destroyAfterEach: false },
});
broadcastService = TestBed.inject(MsalBroadcastService);
}
Expand Down Expand Up @@ -442,4 +443,29 @@ describe("MsalBroadcastService", () => {
InteractionType.Redirect
);
});

it("automatically sets inProgress to None when handleRedirectPromise returns without emitting an event", (done) => {
const expectedInProgress = [
InteractionStatus.Startup,
InteractionStatus.None,
];
let index = 0;

subscription = broadcastService.inProgress$.subscribe((result) => {
expect(result).toEqual(expectedInProgress[index]);
if (index === expectedInProgress.length - 1) {
done();
} else if (expectedInProgress[index] === InteractionStatus.Startup) {
index++;
broadcastService.resetInProgressEvent();
} else {
index++;
}
});

eventHandler.emitEvent(
EventType.INITIALIZE_START,
InteractionType.Redirect
);
});
});
9 changes: 9 additions & 0 deletions lib/msal-angular/src/msal.broadcast.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@ export class MsalBroadcastService {
}
});
}

/**
* Resets inProgress state to None
*/
resetInProgressEvent(): void {
if (this._inProgress.value === InteractionStatus.Startup) {
this._inProgress.next(InteractionStatus.None);
}
}
}
1 change: 1 addition & 0 deletions lib/msal-angular/src/msal.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function initializeMsal(providers: any[] = []) {
RouterTestingModule.withRoutes([]),
],
providers: [MsalGuard, MsalService, MsalBroadcastService, ...providers],
teardown: { destroyAfterEach: false },
});

authService = TestBed.inject(MsalService);
Expand Down
1 change: 1 addition & 0 deletions lib/msal-angular/src/msal.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function initializeMsal() {
},
Location,
],
teardown: { destroyAfterEach: false },
});

interceptor = TestBed.inject(MsalInterceptor);
Expand Down
1 change: 1 addition & 0 deletions lib/msal-angular/src/msal.navigation.client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("MsalCustomNaviationClient", () => {
MsalService,
MsalGuard,
],
teardown: { destroyAfterEach: false },
});
authService = TestBed.inject(MsalService);
navigationClient = TestBed.inject(MsalCustomNavigationClient);
Expand Down
1 change: 1 addition & 0 deletions lib/msal-angular/src/msal.redirect.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function initializeMsal() {
declarations: [MsalRedirectComponent],
imports: [MsalModule.forRoot(MSALInstanceFactory(), null, null)],
providers: [],
teardown: { destroyAfterEach: false },
});

authService = TestBed.inject(MsalService);
Expand Down
26 changes: 21 additions & 5 deletions lib/msal-angular/src/msal.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { TestBed } from "@angular/core/testing";
import {
AuthenticationResult,
AuthError,
InteractionStatus,
InteractionType,
Logger,
PublicClientApplication,
SilentRequest,
} from "@azure/msal-browser";
import { MsalModule, MsalBroadcastService, MsalService } from "./public-api";
import { takeLast } from "rxjs/operators";

let authService: MsalService;
let broadcastService: MsalBroadcastService;
Expand All @@ -30,6 +32,7 @@ function initializeMsal() {
}),
],
providers: [MsalService, MsalBroadcastService],
teardown: { destroyAfterEach: false },
});

authService = TestBed.inject(MsalService);
Expand Down Expand Up @@ -198,7 +201,7 @@ describe("MsalService", () => {

authService.ssoSilent(request).subscribe({
error: (error: AuthError) => {
expect(error.message).toBe(sampleError.message);
expect(error.errorMessage).toBe(sampleError.errorMessage);
expect(
PublicClientApplication.prototype.ssoSilent
).toHaveBeenCalledWith(request);
Expand Down Expand Up @@ -259,7 +262,7 @@ describe("MsalService", () => {

authService.acquireTokenSilent(request).subscribe({
error: (error: AuthError) => {
expect(error.message).toBe(sampleError.message);
expect(error.errorMessage).toBe(sampleError.errorMessage);
expect(
PublicClientApplication.prototype.acquireTokenSilent
).toHaveBeenCalledWith(request);
Expand Down Expand Up @@ -339,7 +342,7 @@ describe("MsalService", () => {

authService.acquireTokenPopup(request).subscribe({
error: (error: AuthError) => {
expect(error.message).toBe(sampleError.message);
expect(error.errorMessage).toBe(sampleError.errorMessage);
expect(
PublicClientApplication.prototype.acquireTokenPopup
).toHaveBeenCalledWith(request);
Expand All @@ -350,11 +353,14 @@ describe("MsalService", () => {
});

describe("handleRedirectObservable", () => {
it("success", (done) => {
it("success and resets inProgress event to none", (done) => {
const sampleAccessToken = {
accessToken: "123abc",
};

//@ts-ignore
broadcastService._inProgress.next(InteractionStatus.Startup);

spyOn(PublicClientApplication.prototype, "initialize").and.returnValue(
Promise.resolve()
);
Expand All @@ -379,13 +385,19 @@ describe("MsalService", () => {
expect(
PublicClientApplication.prototype.handleRedirectPromise
).toHaveBeenCalled();
broadcastService.inProgress$.subscribe((result) => {
expect(result).toBe(InteractionStatus.None);
});
done();
});
});

it("failure", (done) => {
it("failure and also resets inProgress event to none", (done) => {
const sampleError = new AuthError("123", "message");

//@ts-ignore
broadcastService._inProgress.next(InteractionStatus.Startup);

spyOn(PublicClientApplication.prototype, "initialize").and.returnValue(
Promise.resolve()
);
Expand All @@ -408,6 +420,10 @@ describe("MsalService", () => {
expect(
PublicClientApplication.prototype.handleRedirectPromise
).toHaveBeenCalled();
broadcastService.inProgress$.pipe(takeLast(1)).subscribe((result) => {
console.log("failure result", result);
expect(result).toBe(InteractionStatus.None);
});
done();
},
});
Expand Down
10 changes: 8 additions & 2 deletions lib/msal-angular/src/msal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { Inject, Injectable } from "@angular/core";
import { Inject, Injectable, Injector } from "@angular/core";
import { Location } from "@angular/common";
import {
IPublicClientApplication,
Expand All @@ -21,6 +21,7 @@ import { Observable, from } from "rxjs";
import { IMsalService } from "./IMsalService";
import { name, version } from "./packageMetadata";
import { MSAL_INSTANCE } from "./constants";
import { MsalBroadcastService } from "./msal.broadcast.service";

@Injectable()
export class MsalService implements IMsalService {
Expand All @@ -29,7 +30,8 @@ export class MsalService implements IMsalService {

constructor(
@Inject(MSAL_INSTANCE) public instance: IPublicClientApplication,
private location: Location
private location: Location,
private injector: Injector
) {
const hash = this.location.path(true).split("#").pop();
if (hash) {
Expand Down Expand Up @@ -59,6 +61,10 @@ export class MsalService implements IMsalService {
.then(() =>
this.instance.handleRedirectPromise(hash || this.redirectHash)
)
.finally(() => {
// update inProgress state to none
this.injector.get(MsalBroadcastService).resetInProgressEvent();
})
);
}
loginPopup(request?: PopupRequest): Observable<AuthenticationResult> {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.