The provided functions are injected at application startup and executed during app initialization. If any of these functions returns a Promise or an Observable, initialization does not complete until the Promise is resolved or the Observable is completed.
The APP_INITIALIZER is invoked and the promise is resolved.
The MSAL_GUARD_CONFIG factory is invoked (along with the other MSAL components)
The application loads successfully in the browser.
Actual Results
The APP_INITIALIZER is invoked, but the Promise has not resolved.
The MSAL_GUARD_CONFIG factory is invoked.
An Error is thrown because the startup config values are unavailable because the APP_INITIALIZER promise hasn't yet resolved.
The APP_INITIAIZER promise is resolved, and the config values are ready.
The application does not load in the browser due to the previous error.
Notes
This only appears to be an issue when the default route is protected with canActivate: [MsalGuard].
If the default route is not protected, the application loads as expected.
Error Message
This issue arises early in the application bootstrapping before the MSAL gets involved. The problem is that the MSAL_GUARD_CONFIG factory method is being invoked before the promise of the APP_INITIALIZER has been resolved.
The error message that occurs is due to the environment-specific configuration values not being present because the MSAL bootstrapping happens before the APP_INITIALIZER promise resolves.
MSAL Logs
[vite] connecting...
app.config.ts:11 appInit called
app.config.ts:77 MSALGuardConfigFactory called
core.mjs:6531 ERROR Error: Environment is uninitialized
at _AppStartService.getEnv (app-start.service.ts:40:13)
at Object.MSALGuardConfigFactory [as useFactory] (app.config.ts:78:37)
at Object.factory (core.mjs:3322:38)
at core.mjs:3219:47
at runInInjectorProfilerContext (core.mjs:866:9)
at R3Injector.hydrate (core.mjs:3218:21)
at R3Injector.get (core.mjs:3082:33)
at injectInjectorOnly (core.mjs:1100:40)
at ɵɵinject (core.mjs:1106:42)
at Object.MsalGuard_Factory [as factory] (azure-msal-angular.mjs:339:34)
handleError @ core.mjs:6531
Show 1 more frame
Show less
app-start.service.ts:29 got config: Object
core.mjs:29749 Angular is running in development mode.
client.ts:173 [vite] connected.
Core Library
MSAL.js (@azure/msal-browser)
Core Library Version
3.10.0
Wrapper Library
MSAL Angular (@azure/msal-angular)
Wrapper Library Version
3.0.13
Public or Confidential Client?
Public
Description
This is a project that demonstrates that the
MSAL_GUARD_CONFIG
factory is called before theAPP_INITIALIZER
promise is resolved.From Angular's documentation
Steps to reproduce
npm i
npm start
Expected Results
APP_INITIALIZER
is invoked and the promise is resolved.MSAL_GUARD_CONFIG
factory is invoked (along with the other MSAL components)Actual Results
APP_INITIALIZER
is invoked, but thePromise
has not resolved.MSAL_GUARD_CONFIG
factory is invoked.APP_INITIALIZER
promise hasn't yet resolved.APP_INITIAIZER
promise is resolved, and the config values are ready.Notes
canActivate: [MsalGuard]
.Error Message
This issue arises early in the application bootstrapping before the MSAL gets involved. The problem is that the
MSAL_GUARD_CONFIG
factory method is being invoked before the promise of theAPP_INITIALIZER
has been resolved.The error message that occurs is due to the environment-specific configuration values not being present because the MSAL bootstrapping happens before the
APP_INITIALIZER
promise resolves.MSAL Logs
[vite] connecting... app.config.ts:11 appInit called app.config.ts:77 MSALGuardConfigFactory called core.mjs:6531 ERROR Error: Environment is uninitialized at _AppStartService.getEnv (app-start.service.ts:40:13) at Object.MSALGuardConfigFactory [as useFactory] (app.config.ts:78:37) at Object.factory (core.mjs:3322:38) at core.mjs:3219:47 at runInInjectorProfilerContext (core.mjs:866:9) at R3Injector.hydrate (core.mjs:3218:21) at R3Injector.get (core.mjs:3082:33) at injectInjectorOnly (core.mjs:1100:40) at ɵɵinject (core.mjs:1106:42) at Object.MsalGuard_Factory [as factory] (azure-msal-angular.mjs:339:34) handleError @ core.mjs:6531 Show 1 more frame Show less app-start.service.ts:29 got config: Object core.mjs:29749 Angular is running in development mode. client.ts:173 [vite] connected.
Network Trace (Preferrably Fiddler)
MSAL Configuration
Relevant Code Snippets
Reproduction Steps
npm i
npm start
Expected Behavior
APP_INITIALIZER
is invoked and the promise is resolved.MSAL_GUARD_CONFIG
factory is invoked (along with the other MSAL components)Identity Provider
Entra ID (formerly Azure AD) / MSA
Browsers Affected (Select all that apply)
Chrome, Firefox, Edge, Safari
Regression
No response
Source
External (Customer)