Closed anisabboud closed 3 years ago
For me I can't even get close as i get these errors. Then again I am lazy loading my modules
ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard]: StaticInjectorError(Platform: core)[AngularFireAuthGuard]: NullInjectorError: No provider for AngularFireAuthGuard! NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard]: StaticInjectorError(Platform: core)[AngularFireAuthGuard]: NullInjectorError: No provider for AngularFireAuthGuard!
@JGSolutions just add it in the providers of your app.module.ts, like a normal guard
@Tyldar Thanks it works
For me I can't even get close as i get these errors. Then again I am lazy loading my modules
ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard]: StaticInjectorError(Platform: core)[AngularFireAuthGuard]: NullInjectorError: No provider for AngularFireAuthGuard! NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard]: StaticInjectorError(Platform: core)[AngularFireAuthGuard]: NullInjectorError: No provider for AngularFireAuthGuard!
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.
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?
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.
Same issue here.
Is there any news about this? This is currently a showstopper in my project :-(
Still waiting for a fix for this.
I have the same problem, I just want a auth guard like :
// app-routing.module.ts
import { AngularFireAuthGuard, loggedIn, redirectLoggedInTo } from '@angular/fire/auth-guard';
const redirectLoggedInToHome = redirectLoggedInTo(['/']);
const routes: Routes = [
{ path: '', component: HomeComponent },
// { path: 'search', component: SearchComponent },
// { path: 'search:query', component: SearchComponent },
{ path: 'login', component: LoginComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInToHome }},
{ path: 'register', component: RegisterComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInToHome }},
{ path: 'my-account', component: AccountComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: loggedIn }},
{ path: 'events', component: EventsComponent },`
];
but have this when i'm logged in and I try to go in '/login' for example :
I just confused loggedIn()
and redirectUnauthorizedTo()
// app-routing.module.ts
const redirectLoggedInToHome = () => redirectLoggedInTo(['/']);
const redirectUnauthorizedToHome = () => redirectUnauthorizedTo(['/']);
// app-routing.module.ts
{ path: 'login', component: LoginComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInToHome }},
{ path: 'register', component: RegisterComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInToHome }},
{ path: 'my-account', component: AccountComponent, canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectUnauthorizedToHome }},
Don't forget to add AngularFireAuthGuard in app providers and imports
// app.module.ts
imports: [
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule,
AngularFireAuthModule,
AngularFireStorageModule,
],
providers: [
AuthService,
AngularFireAuthGuard,
],
The solution posted in issue #2099 works for me in production and non-production mode.
https://github.com/angular/angularfire2/issues/2099#issuecomment-503403712
I can confirm the solution in #2099 works for me too. Thanks for sharing @devinshoemaker!
Is there gonna be proper fix ?
Waiting for a proper fix too.
Same problem here. the solution in #2099 works for me but how can I make it works for redirectLoggedInTo
?
Faced same problem. #2099 solution worked for me.. Can you please update https://github.com/angular/angularfire2/blob/master/docs/auth/router-guards.md documentation with this issue detail so other developers can make decision accordingly.
Despite adding AngularFireAuthGuard
and AngularFireAuthGuardModule
to app.module.ts, I am encountering the NullInjectorError but it complains there isn't a provider for Router
, even though I am importing RouterModule
in there as well!
errors.ts:30 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard -> Router]:
StaticInjectorError(Platform: core)[AngularFireAuthGuard -> Router]:
NullInjectorError: No provider for Router!
NullInjectorError: StaticInjectorError(AppModule)[AngularFireAuthGuard -> Router]:
StaticInjectorError(Platform: core)[AngularFireAuthGuard -> Router]:
NullInjectorError: No provider for Router!
I have demonstrated this on a StackBlitz Project: https://stackblitz.com/edit/angular-scftvu
I am experiencing a similar issue as @benbabics, but only with SSR.
Here's the stack trace:
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(HomePageModule)[AngularFireAuthGuard -> Router -> Router -> Router -> Router]:
NullInjectorError: No provider for Router!
NullInjectorError: R3InjectorError(HomePageModule)[AngularFireAuthGuard -> Router -> Router -> Router -> Router]:
NullInjectorError: No provider for Router!
You should add
AngularFireAuthGuardModule
to your imports for the correct importing.
Thanks to @ArrowsOff for this tip; didn't realize AuthGuard was its own module; importing in root resolved the NullInjectorError
, but then I was hit w/ the source.lift is not a function
issue Id'd in #2099, resolved by these two posts in that thread.
Sure would be nice if this were documented...
I am experiencing the exact same issue as @benbabics when running my project on StackBlitz. However, when serving the same project on my desktop everything seems to work fine 😕
@JGSolutions just add it in the providers of your app.module.ts, like a normal guard
that also worked for me.
I solved this with the below code:
// app.module.ts
imports: [
...
provideAuth(() => {
if (Capacitor.isNativePlatform()) {
return initializeAuth(getApp(), {
persistence: indexedDBLocalPersistence,
});
} else {
return getAuth();
}
}),
]
Version info
Angular:
8.0.1
Firebase:6.2.0
AngularFire:5.2.1
tsconfig.json"target": "es5"
Steps to set up and reproduce
...canActivate(redirectUnauthorizedTo(['login']))
like below: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 withng build --prod
.