Sergio
Sergio

Reputation: 127

Get Provider UID in Firebase using Angular2

Im trying to access the facebook uid using firebase providers in Angular2, but I can´t get the user uid from the facebook provider.

When I use authState.uid It shows me the firebase uid and not the facebook uid, the documentation is outdated and the authState.facebook.uid is deprecated so I don´t know the right way to get the facebook user id.

Through internet I saw that it´s a new way to get the facebook user id using something like authState.providerData.uid but in the console I get undefined.

Here is my code:

export class AppComponent {
  user: Observable<firebase.User>;
  displayName;
  photoURL;


  constructor(public afAuth: AngularFireAuth, private db: AngularFireDatabase, private http: Http) {
    this.user = afAuth.authState;
  }

  ngOnInit() {
    this.afAuth.authState.subscribe(authState => {
      if (!authState) {
        console.log('NOT LOGGED IN');
        this.displayName = null;
        this.photoURL = null;
        return;
      }
      let userRef = this.db.object('/users/' + authState.uid)
      userRef.subscribe(user => {
        let url = `https://graph.facebook.com/v2.8/${authState.uid}?fields=first_name,last_name&access_token=${user.accessToken}`;
        this.http.get(url).subscribe(response => {
          let user = response.json();
          userRef.update({
            firstName: user.first_name,
            lastName: user.last_name
          });
        });
      });


        this.displayName = authState.displayName;
        this.photoURL = authState.photoURL;
        console.log(authState.uid);
    });
  }

Thanks for your time and help :)

Upvotes: 0

Views: 163

Answers (1)

Sergio
Sergio

Reputation: 127

Ok so I solved this way, I don´t know if it is the best way to do it but It works for me...

To get the Facebook uid instead of used authState.facebook.uid (I think this is deprecated) I´m now using authState.providerData[0].uid

I´ve tried with authState.providerData.uid but it didnt work so I put the [0] to get into the array and get the uid.

Upvotes: 0

Related Questions