Angular 2 router - enable auth using angularfire2

I have a route where I want to wait until I get an auth object from firebase. Here is the code I have:

Route

{ path: 'random', component: RandomComponent, resolve: { auth: AuthService } } 

AuthService

 @Injectable() export class AuthService implements Resolve<any> { constructor(private af: AngularFire) { } resolve(): Observable<any> { return this.af.auth.take(1); } } 

Randomcomponent

 export class RandomComponent { constructor(private route: ActivatedRoute) { console.log(this.route.snapshot.data); } } 

Oddly enough, this is logs Object { } , therefore an empty object. If I modify AuthService.resolve to return Observable.of('whatever') , then it is registered Object { auth: "whatever" } , so I'm sure this part of the code works, simply because angularfire2 auth observable does not work in in this case. (I'm really not an observable expert, so maybe I'm doing something wrong).

If I do this.af.auth.take(1).subscribe(auth => console.log(auth)); , it registers the auth object, so the part also works.

So what causes this problem? What am I doing wrong?

(Using the latest router, angular2 and angularfire2.)

+5
source share
1 answer

This should be this.af.user.pipe(take(1)) or this.af.authState.pipe(take(1)) .

0
source

All Articles