Skip to content

AngularFire router guards redirectUnauthorizedTo not working with ng build --prod #2114

@anisabboud

Description

@anisabboud

Version info

Angular: 8.0.1
Firebase: 6.2.0
AngularFire: 5.2.1
tsconfig.json "target": "es5"

Steps to set up and reproduce

const appRoutes: Routes = [
  {
    path: '',
    ...canActivate(redirectUnauthorizedTo(['login'])),
    children: [
      { path: 'calendar', component: CalendarComponent },
      { path: '',
        redirectTo: 'calendar',
        pathMatch: 'full'
      },
    ],
  },
  {
    path: 'login',
    component: LoginComponent,
    ...canActivate(redirectLoggedInTo([''])),
  },
  { path: '**',
    redirectTo: '/',
    pathMatch: 'full'
  },
];
  • Everything works great with ng serve or ng build, but with ng build --prod, the router guard redirectUnauthorizedTo(['login']) stops working. I.e., the user isn't redirected to /login, and can access the protected routes without logging in.

Debug output

There are no errors in the console. The redirection just doesn't occur.
Using RouterModule.forRoot(appRoutes, { enableTracing: true }), confirms that.

With ng build (redirection to /login works):
image

With ng build --prod:
image

Notice that logging the AngularFireAuth.user observable (in yellow) shows that in the first case, it evaluated to null before the routing ended so the guard worked properly.
However, in the second case, the routing was performed before the auth state was evaluated (the observable hasn't emitted), and the guard didn't wait for it so it didn't work.

Expected behavior

Route guards should work with ng build --prod.

Actual behavior

Route guards work with ng build but not with ng build --prod.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions