Skip to content

Commit

Permalink
Merge pull request #3012 from AzureAD/angular-guard-await-redirect
Browse files Browse the repository at this point in the history
Await loginRedirect in MSAL Guard to prevent race conditions
  • Loading branch information
jasonnutter authored Feb 17, 2021
2 parents e94403a + d2c0aa0 commit c9da47e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Await loginRedirect in MSAL Guard to prevent race conditions",
"packageName": "@azure/msal-angular",
"email": "janutter@microsoft.com",
"dependentChangeType": "patch"
}
65 changes: 35 additions & 30 deletions lib/msal-angular/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml', 'coverage-istanbul'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'],
singleRun: true,
restartOnFileChange: true,
concurrency: 1
});
config.set({
basePath: "",
frameworks: ["jasmine", "@angular-devkit/build-angular"],
plugins: [
require("karma-jasmine"),
require("karma-chrome-launcher"),
require("karma-jasmine-html-reporter"),
require("karma-coverage-istanbul-reporter"),
require("@angular-devkit/build-angular/plugins/karma")
],
client: {
jasmine: {
failSpecWithNoExpectations: true
},
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require("path").join(__dirname, "./coverage"),
reports: ["html", "lcovonly", "text-summary"],
fixWebpackSourcePaths: true
},
reporters: ["progress", "kjhtml", "coverage-istanbul"],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ["ChromeHeadless"],
singleRun: true,
restartOnFileChange: true,
concurrency: 1
});
};
9 changes: 5 additions & 4 deletions lib/msal-angular/src/msal.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ describe('MsalGuard', () => {
})
));

const listener = jasmine.createSpy();
guard.canActivate(routeMock, routeStateMock).subscribe(listener);
expect(listener).toHaveBeenCalledWith(false);
done();
guard.canActivate(routeMock, routeStateMock)
.subscribe(result => {
expect(result).toBeFalse();
done();
});
});

it("canActivateChild returns true with logged in user", (done) => {
Expand Down
8 changes: 5 additions & 3 deletions lib/msal-angular/src/msal.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ export class MsalGuard implements CanActivate, CanActivateChild, CanLoad {

this.authService.getLogger().verbose("Guard - logging in by redirect");
const redirectStartPage = this.getDestinationUrl(url);
this.authService.loginRedirect({
return this.authService.loginRedirect({
redirectStartPage,
...this.msalGuardConfig.authRequest
} as RedirectRequest);
return of(false);
} as RedirectRequest)
.pipe(
map(() => false)
);
}

/**
Expand Down

0 comments on commit c9da47e

Please sign in to comment.