angularfire: AngularFire router guards redirectUnauthorizedTo not working with ng build --prod
Version info
Angular: 8.0.1
Firebase: 6.2.0
AngularFire: 5.2.1
tsconfig.json "target": "es5"
Steps to set up and reproduce
- Setup a Firebase project with auth router guards https://github.com/angular/angularfire2/blob/master/docs/auth/router-guards.md and https://github.com/RaphaelJenni/FirebaseUI-Angular
- Use
...canActivate(redirectUnauthorizedTo(['login']))
like below:
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
orng build
, but withng build --prod
, the router guardredirectUnauthorizedTo(['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):
With ng build --prod
:
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
.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 24
- Comments: 22
Same issue here, works like it should with ng serve and after ng build --prod all routes are accessible. Is there a solution for this?
Same issue here.
The solution posted in issue #2099 works for me in production and non-production mode.
https://github.com/angular/angularfire2/issues/2099#issuecomment-503403712
Yup I have the same issue as @anisabboud. Works perfectly in other circumstances, but on a production build the AuthGuards don’t run. Also tried with a custom guard with the same result. Thank you.
You should add
AngularFireAuthGuardModule
to your imports for the correct importing.@anisabboud I came here with the same issue, seems like something is not working when minified.
@JGSolutions just add it in the providers of your app.module.ts, like a normal guard
I have the same problem, I just want a auth guard like :
but have this when i’m logged in and I try to go in ‘/login’ for example :
EDIT
I just confused
loggedIn()
andredirectUnauthorizedTo()
Don’t forget to add AngularFireAuthGuard in app providers and imports
I can confirm the solution in #2099 works for me too. Thanks for sharing @devinshoemaker!
Despite adding
AngularFireAuthGuard
andAngularFireAuthGuardModule
to app.module.ts, I am encountering the NullInjectorError but it complains there isn’t a provider forRouter
, even though I am importingRouterModule
in there as well!I have demonstrated this on a StackBlitz Project: https://stackblitz.com/edit/angular-scftvu
Same problem here. the solution in #2099 works for me but how can I make it works for
redirectLoggedInTo
?Is there gonna be proper fix ?