Angular app authentication fails because the nonce is incorrect

Viewed 196

I have an Angular app that is using angular-oauth2-oidc to manage the authentication for our Angular app, and the auth server is Identity Server 4. Earlier this week the application stopped authenticating properly. After entering the username and password, the auth server redirects back to the application and then gets stuck in a loop of authenticating over and over again. Sometimes it loops two or three times, sometimes it's 10+ times. When it does eventually stop looping, whether on its own or by stopping it manually by clicking the "X" button in the URL bar, I can refresh the page and everything loads as it should.

The error in the console comes from validating the tokens, and that the nonce is incorrect:

Error validating tokens
Wrong nonce: Rmh...Zm
{error: Error: Uncaught (in promise): Wrong nonce: Rmh…}

I'm not sure why the nonce is incorrect, and what is causing the issue. Here's the setup for the authentication in the app.component.ts file:

this._oauth.configure(environment.authModuleConfig);
this._oauth
  .loadDiscoveryDocumentAndLogin()
  .then((res) => {
    if (res) {
      this._oauth.setupAutomaticSilentRefresh();
      
      // do custom logic
    }
  })
  .catch((err) => {
    console.error("loadDiscoveryDocumentAndLogin error", err);
    // if (!this._oauth.hasValidAccessToken()) {
    //   this._oauth.tryLogin();
    // }
  });

I have tried removing the setupAutomaticSilentRefresh method call, based on some other questions that I've seen online, but it doesn't appear to have had an effect. I also saw some people say that trying to login again in the .catch handler can work. There is no noticeable difference there either.

I'm completely lost as to why the nonce is incorrect at times and causing the errors. Any hints as to what to check next would be greatly appreciated.

0 Answers
Related