最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

openid connect - token is always null with angular-auth-oidc-client 13.1.0 - Stack Overflow

programmeradmin2浏览0评论

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();
  }
}

发布评论

评论列表(0)

  1. 暂无评论