Looking at this solution (angular2), I have been trying to get the same functionality to work in angular4 and coming up short. The error I get is:
_Property 'subscribe' does not exist on type 'Auth'_
Here is some sample code (not working):
angularFire.ts (provider):
import {Injectable} from "@angular/core";
import { Observable } from 'rxjs/Observable';
import { FirebaseObjectFactoryOpts } from "angularfire2/interfaces";
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
@Injectable()
export class AF {
public messages: FirebaseListObservable<any>;
public users: FirebaseListObservable<any>;
public displayName: string;
public email: string;
public user: Observable<firebase.User>;
constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.afAuth.auth.subscribe(
(auth) => {
if (auth != null) {
this.user = db.list.object('users/' + auth.uid);
}
});
this.messages = db.list('messages');
this.users = db.list('users');
} ...
}
Any pointers would be great!
try this.afAuth.authState.subscribe
@harrylincoln This is apart of the new API redesign. See the new docs for updates. @WillGeller is correct in that afAuth.authState is an observable of a firebase.User.
Note: If you use async to access the data on the user observable after logout you will get Firebase permission error. AngulareFire will not destroy the subscription after logout. Right davidest.
Most helpful comment
try this.afAuth.authState.subscribe