Angularfire: Get User Auth Profile Info in Firebase using Angular4 and Angularfire2 (v4)

Created on 7 May 2017  路  3Comments  路  Source: angular/angularfire

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!

Most helpful comment

try this.afAuth.authState.subscribe

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jnupeter picture jnupeter  路  3Comments

mypark picture mypark  路  3Comments

cre8 picture cre8  路  3Comments

Leanvitale picture Leanvitale  路  3Comments

aucevica picture aucevica  路  3Comments