In a legacy application, upgraded Angular from v11.x to v13.0.x (restrictions due to other depndencies). Below code uses Angular v13.0.1 and angular-auth-oidc-client v13.1.0
During the upgrade had to change the config properties from stsServer to authority and authWellknownEndpoint to authWellknownEndpointUrl to comply with the interface.
Why is value of this.oidcSecurityService.getIdToken() is always logging as null?
#auth-config.module.ts
import { NgModule } from '@angular/core';
import { AuthModule, OpenIdConfiguration, } from 'angular-auth-oidc-client';
const authConfig: OpenIdConfiguration = { // Use OpenIdConfiguration
authority: '.0',
authWellknownEndpointUrl: '.0/.well-known/openid-configuration',
redirectUrl: `${window.location.origin}${window.location.pathname}`,
clientId: 'client_id',
scope: 'openid profile email User.Read offline_access',
responseType: 'code',
silentRenew: true,
silentRenewUrl: `${window.location.origin}/silent-renew.html`,
maxIdTokenIatOffsetAllowedInSeconds: 1800,
useRefreshToken: true,
};
@NgModule({
imports: [
AuthModule.forRoot({
config: authConfig,
}),
],
exports: [AuthModule],
})
export class AuthConfigModule {}
#auth.service.module.ts
import { Injectable, OnInit } from '@angular/core';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class AuthService implements OnInit {
constructor(private oidcSecurityService: OidcSecurityService,protected httpClient: HttpClient,) {
}
ngOnInit(): void {
}
get userData() {
return this.oidcSecurityService.userData$;
}
get token() {
console.log("this.oidcSecurityService :"+this.oidcSecurityService);
console.log("token in getToken : "+this.oidcSecurityService.getIdToken());
sessionStorage.setItem("TOKEN_KEY", this.oidcSecurityService.getIdToken());
return this.oidcSecurityService.getIdToken();
}
signIn(): Promise<void>{
return new Promise(resolve =>
this.oidcSecurityService.checkAuth().subscribe((auth: any) => {
console.log('is authenticated in signIn', auth);
// debugger
if (!auth) {
this.oidcSecurityService.authorize();
} else {
return resolve();
}
})
);
}
signOut() {
this.oidcSecurityService.logoff();
}
}