Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Getting null on checkAuthIncludingServer() while using it on auth guard in Angular #1652

Open
frk93 opened this issue Jan 12, 2023 · 0 comments
Labels

Comments

@frk93
Copy link

frk93 commented Jan 12, 2023

What Version of the library are you using?
15.0.2

Question
There are some cases where I got null from checkAuthIncludingServer() function. I am using Angular 15. I am using the checkAuthIncludingServer() function in my guard ( canActivate ) and it hits the function every time I change the pages where I have this guard in place. I noticed when I do it fast or when there is token change I sometimes got null response from the function. I am not sure what causing it, but if someone has more detailed explanation I would appreciate that.

What I am thinking at this point that, while doing it fast and when it ask the authority get a new token and if I still moving through pages of my web application then it fails and unable to retrieve it at that point in time.

My config

@NgModule({
  imports: [
    AuthModule.forRoot({
      config: {
        authority: environment.authConfig.authority,
        redirectUrl: window.location.origin,
        postLogoutRedirectUri: window.location.origin + '/sign-in',
        clientId: environment.authConfig.clientId,
        scope: environment.authConfig.scope,
        responseType: environment.authConfig.responseType,
        unauthorizedRoute: '/sign-in',
        silentRenew: true,
        useRefreshToken: true,
        startCheckSession: environment.platform === 'pwa',
        renewTimeBeforeTokenExpiresInSeconds: 20,
        secureRoutes: [environment.backendBaseUrl],
        configId: environment.configId,
        //       logLevel: LogLevel.Debug,
        ignoreNonceAfterRefresh: true,
      },
    }),
  ],
  providers: [{ provide: AbstractSecurityStorage, useClass: AuthStorage }],
  exports: [AuthModule],
})
export class AuthConfigModule {}

How I solved it at the moment is doing a work around like this

// this code runs in my auth guard to check for auth

return await this.zone.run(async () => {
      try {
        let result: LoginResponse;
        result = await firstValueFrom(this.oidcSecurityService.checkAuthIncludingServer());
        // Workaround 
        if (result === null) {
          return await firstValueFrom(
            this.oidcSecurityService.isAuthenticated$.pipe(
              take(1),
              map((isAuthenticated) => isAuthenticated.isAuthenticated)
            )
          );
        }

        if (result?.isAuthenticated) {
          return true;
        }
        if (result?.errorMessage) {
          this.authenticated = false;
        }
      } catch (error) {
        this.authenticated = false;
        return false;
      }
    });

I am open for suggestion and leads to understanding of the problem.

@frk93 frk93 added the question label Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant