angular-oauth2-oidc token always invalid after log in

Issue

I am trying to follow the guide on the documentation https://github.com/manfredsteyer/angular-oauth2-oidc.

I have the following config in my constructor for the service:

authCodeFlowConfig: AuthConfig = {
   issuer: 'https://demo.identityserver.io',
   redirectUri: window.location.origin + '/home',
   clientId: 'spa',
   responseType: 'code',
   scope: 'openid profile email offline_access api',
   showDebugInformation: true
 };

this.oauthService.configure(this.authCodeFlowConfig);

(i have also tried with a redirect url of index.html but that didnt seem to make a difference)

Logging in seems to work just fine i get the redirect to the page log in and get redirected home:

    this.oauthService.loadDiscoveryDocumentAndTryLogin().then((response) => {
      if (!this.oauthService.hasValidIdToken() || !this.oauthService.hasValidAccessToken()) {
         this.oauthService.initCodeFlow();
      }
    }).catch((err) => {
      console.log(err);
    });

However the following still returns false, false, null:

    const claims = this.oauthService.getIdentityClaims();
    console.log(this.oauthService.hasValidIdToken());
    console.log(this.oauthService.hasValidAccessToken());
    console.log(claims);

I have a code= in my url have no other changes to the service and everything seems to have logged me in.
I’m expecting to have done something stupid or misunderstood what is going on but any advice would be helpful.

Solution

Turns out i wasn’t doing it correctly.

I wasn’t calling the load discovery document in my constructor as i should have done.

If the page reloads i dont have the discovery document to check my token against and hence it is invalid.

Answered By – Damian Fox

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.