Search code examples
androidfacebookangularionic2cordova-plugins

Retrieve name from Facebook using cordova-plugin-facebook4


I need to retrieve first and last name from Facebook after user register successfuly.

Actually i have:

Facebook
            .login(['email'])
            .then( (response) => {
               /*
              Here i need to get name and last name for later usage
              */
                let facebookCredential = firebase.auth.FacebookAuthProvider
                    .credential(response.authResponse.accessToken);

                firebase.auth().signInWithCredential(facebookCredential)
                    .then((success) => {
                        this.userProfile = success;

                        this.storage.set('facebookMethod_email', this.userProfile.email);

                        this.nav.setRoot(HomePage,
                            {
                            facebookUser: this.userProfile
                            },
                            {
                                animate: true,
                                direction: 'forward'
                            });
                    })
                    .catch((error) => {
                        let prompt = this.alertCtrl.create({
                        title: this.FAIL_TITLE,
                        subTitle: this.AUTH_FAILED_SUBTITLE,
                        buttons: [this.ACCEPT_BUTTON]
                        });
                    prompt.present();
                    });
            })
            .catch((error) => {
                console.log(error)
            });

And it works perfectly. It first do the register flow with facebook and then, register in firebase.. But i need to get those user attributtes from facebook to store this information separately.

Thank's in advance! Ivan.


Solution

  • See complete project here https://github.com/aaronksaunders/AngularFire2-Ionic2-Facebook

    /**
       * 
       * get the profile information from the Facebook user to add it to the database
       * 
       * @returns
       * 
       * @memberOf HomePage
       */
      _FBUserProfile() {
    
        return new Promise((resolve, reject) => {
          Facebook.api('me?fields=id,name,email,first_name,last_name,picture.width(100).height(100).as(picture_small),picture.width(720).height(720).as(picture_large)', [])
            .then((profileData) => {
              console.log(JSON.stringify(profileData));
              return resolve(profileData);
            }, (err) => {
              console.log(JSON.stringify(err));
              return reject(err);
            });
        });
      }