One infinite redirect loop in an Angular application

canactivate infinite loop
angular route data

I'm doing an Angular application with the following routing:

const routes: Routes = [
  {
    path: '',
    component: LoginLayoutComponent,
    children: [
      {
        path: '',
        redirectTo: 'login',
        pathMatch: 'full'
      },
      {
        path: 'login',
        component: LoginComponent
      }
    ]
  },
  {
    path: '',
    component: HomeLayoutComponent,
    canActivate: [AuthGuardService],
    children: [
      {
        path: 'users',
        component: UsersComponent,
      },
      {
        path: 'detail/:id',
        component: UserComponent,
      },
      {
        path: 'dashboard',
        component: DashboardComponent,
        data: {
          expectedRole: 'admin'
        }
      },
      {
        path: 'home',
        loadChildren: './views/home/home.module#HomeModule',
        data: {
          preload: true,
          delay: false
        }
      },
      {
        path: 'error',
        component: ErrorComponent
      },
    ]
  },
];

If I'm not logged in and I request any secured url, like for example http://localhost:4200/users or http://localhost:4200/dashboard then there is a redirect to the http://localhost:4200/ and the application goes into an infinite loop. If I'm logged in then it works fine.

The browser console displays the following message Navigation triggered outside Angular zone.

Here is my auth guard service:

  canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
    const expectedRole = route.data.expectedRole ? route.data.expectedRole : null;
    const tokenPayload = this.tokenService.getDecodedAccessToken();
    return this.authService.isAuthenticated()
    .pipe(
      map(isAuth => {
        if (!isAuth) {
          this.router.navigate(['login']);
          return false;
        } else {
          return true;
        }
      }),
      catchError((error, caught) => {
        return of(false);
      })
    );
  }

  canLoad(): Observable<boolean> {
    if (this.authService.isAuthenticated()) {
      return of(true);
    } else {
      return of(false);
    }
  }

I'm on Angular 7

EDIT: The issue is now resolved with the following auth guard:

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.authService.isAuthenticated()
    .pipe(
      map(isAuthenticated => {
        if (!isAuthenticated) {
          this.authService.setPostLoginRedirectUrl(state.url);
          this.router.navigate(['login']);
          return false;
        } else {
          return true;
        }
      }),
      catchError((error, caught) => {
        return of(false);
      })
    );
  }

and the following routes:

const routes: Routes = [
  {
    path: '',
    component: LoginLayoutComponent,
    children: [
      {
        path: '',
        redirectTo: 'login',
        pathMatch: 'full'
      },
      {
        path: 'login',
        component: LoginComponent
      }
    ]
  },
  {
    path: '',
    component: HomeLayoutComponent,
    canActivateChild: [AuthGuardService],
    children: [
      {
        path: 'users',
        component: UsersComponent,
      },
      {
        path: 'detail/:id',
        component: UserComponent,
      },
      {
        path: 'dashboard',
        component: DashboardComponent,
        data: {
          expectedRole: 'admin'
        }
      },
      {
        path: 'home',
        loadChildren: './views/home/home.module#HomeModule',
        data: {
          preload: true,
          delay: false
        }
      },
      {
        path: 'error',
        component: ErrorComponent
      },
    ]
  },
];

redirectTo value should always have a leading / as it represents the actual route the user should be navigated to.

Change redirectTo: 'login', to redirectTo: '/login', in your Route Config

Also, this:

this.router.navigate(['login']);

should be

this.router.navigate(['/login']);

I'm doing an Angular application with the following routing: const routes: Routes = [ { path: '', component: LoginLayoutComponent, children: [ { path: '', redirectTo:  Infinite redirect between OpenID Connect Application and Azure AD July 29, 2019 July 31, 2019 Bac Hoang [MSFT] Recently I came across an interesting infinite redirection problem between an OpenID Connect (OIDC) Application and Azure AD as demonstrated in the Fiddler screen shot below.


I don't have an exact soution for you, just a few this suggestions for debugging. Try expressing this as:

const routes: Routes = [
{
  path: '',
  component: LoginLayoutComponent,
  children: [
    {
      path: 'login',
      component: LoginComponent
    },
    {
      path: '',
      component: LoginComponent,
      pathMatch: 'full'
    },
  ]
}

to see if it is the route redirect.

Try replacing this with,

this.router.navigateByUrl('/login');

or

this.router.navigate(['', 'login']);

Creating a new ticket as I cannot reproduce using the browser back button. This bug is reproducible in the vanilla example for coexisting angular  In our application we have found that having more simultaneous angular apps running in parallel triggers the issue more commonly, however this bug is reproducible with only one app actively loaded by the single-spa (however with difficultly even in IE11). (Issue does not occur at all with the app running in angular standalone without the spa)


For my problem, the order of import impacts on this error.

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    CommonModule,

    AccountModule, // Load other eager modules first
    AppRoutingModule // Move this AppRouting to the end like this
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

I've noticed one critical issue with a router in "coexisting-angular-microfrontends" project: loaded and rendered I'm getting into an infinite redirect loop. I upgraded all of the single-spa applications in this repo to Angular 8. Authentication Redirection Loop with Angular Application and Azure Active Directory. Recently we ran in to some difficulty with an Angular application that was being retrofitted into a different environment. During the initial development the Angular application had been pushed to Azure for testing. However, the final resting place for the application was on a on-premises sever.


Hi Folks, I'm using Ionic, Angular and Auth0 to make a web app. I've got a problem where my app and auth0 are getting stuck in a redirect loop and I'm not sure why - nor where to start investigating. 4lexnz February 15, 2020, 9:29pm #​1. I've setup the adal and adal-angular v.1.0.10 libraries with my SPA application with mostly great success. I am using webpack, but reference these in my html page in hopes of avoiding global scope issues (though I'd like it to be a dependency).


How to fix infinite login redirect loop in OKta with OpenConnectID in ASP.NET 4.x when an app is deployed to a server. If ever you face a problem like the one I faced above, you can do the following to fix. Software development · How to Fix @angular/compiler-cli” package was not properly installed. This redirect loop is a common symptom to a few different errors, and before we look at some common issues and debug steps, let’s take a look at what this redirect loop actually is, and what is causing it. The high-level process is: The client application issues an authentication challenge (either manually or as the result of a 401)


ruchimodi December 12, 2019, 10:29am #1 When I try to access my application, it redirects to keycloak loginpage(If user is not loagged in), After providing If I refresh the page, It goes in infinite loop(http://localhost:8087/​testapp/index.html? in the Azure portal, create an app with the 'user assignment required' setting set to 'True' insert azure tenant and client id in app.ts; run app (e.g. with lite-server) and login with a user that does not have a role for the application; The app redirects to the login page, but if you are already logged in, it keeps on looping.