-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Is there a suggested way to use the canActivate property on a route while using firebase authentication?
I am using the latest alpha router and trying to implement the authGuard as provided in the docs but I run into an issue with the asynchronous auth observable that returns after the authGuard has completed (this appears to only happen when you refresh the page after having already logged in). The results is that no component shows because the subscription (see below) doesn't complete until after loggedIn returns false. Should I be trying to find a way to stop execution to wait for a response from the observable or is there another solution I should try?
Here is the example of my authGuard:
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private af: AngularFire, private router: Router) {}
canActivate(): boolean {
var loggedIn = false;
console.log("Authenticating");
this.af.auth.subscribe(auth => {
if (auth) {
loggedIn = true;
} else {
this.router.navigate(['/login']);
}
})
return loggedIn;
}
}
I should note that I am using email/password authentication. I noticed there is another issue created about persisting oauth credentials, this issue may run in the same vein as that one.
I could go back to using logic in the template to show components but I prefer using the guard.