I am currently working on integrating Angular Adal for authenticating my application's admin interface with Azure AD. However, I have encountered a challenge with the redirectUri
setting. My goal is to dynamically retrieve the current app's host (location.host
) and use it as the base for the redirectUri
parameter so that it always generates the correct callback URL when communicating with AD.
In an ideal scenario, my configuration in admin.module.ts
would be structured like this:
MsAdalAngular6Module.forRoot({
tenant: '[Removed]',
clientId: '[Removed]',
redirectUri: `https://${location.host}/admin`,
endpoints: {
},
navigateToLoginRequestUrl: false,
cacheLocation: 'localStorage',
postLogoutRedirectUri: `https://${location.host}/logout`,
})
While this setup works perfectly in development mode, the issue arises during production builds due to Angular's AOT compilation process replacing location.host
with null
. Upon researching possible solutions, it was proposed to utilize an InjectionToken
to resolve this value at runtime. Consequently, I've made adjustments in the admin.module.ts
file prior to the @NgModule
declaration :
const REDIRECT_URI = new InjectionToken<string>('REDIRECT_URI');
export function redirectUriFactory() {
console.warn('redirect to', `https://${location.host}/admin`); //debug correct uri
return `https://${location.host}/admin`
}
Subsequently, within my NgModule
, I have implemented:
MsAdalAngular6Module.forRoot({
tenant: '[Removed]',
clientId: '[Removed]',
redirectUri: REDIRECT_URI,
endpoints: {
},
navigateToLoginRequestUrl: false,
cacheLocation: 'localStorage',
postLogoutRedirectUri: `https://${location.host}/logout`,
})
The provider section now includes the definition of REDIRECT_URI
as shown below:
providers: [
...
{ provide: REDIRECT_URI, useFactory: redirectUriFactory }
]
However, I am encountering an error during authentication, as the RedirectUri is being passed as InjectionToken REDIRECT_URI
instead of using the value generated by my redirectUriFactory
function.
Is there a way to successfully retrieve the location.host
within my admin.module.ts
file?
Thank you!