Nativescript-plugin-firebase: Getting Facebook userid/token

Created on 25 Feb 2017  路  8Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

Hi, sorry to post it here, but I didn't find anything in the docs.

To access the Facebook Graph API we need an access token.

The token we get with the firebase.getAuthToken() is the Firebase token.

Is there any way to get the Facebook access token?

Most helpful comment

@abhayastudios is there any way to get the access_token from google?

All 8 comments

I have implemented this functionality in this fork https://github.com/dgomezs/nativescript-plugin-firebase. You can get the access token directly from the result

firebase.login({ type: firebase.LoginType.FACEBOOK, scope: ['public_profile', 'email'] }).then( (result) => { console.log("user " + result.facebookAccessToken); }, (errorMessage) => { console.log("facebook error " + JSON.stringify(errorMessage)); } );

You can call the FBSDKAccessToken class to get it:

declare var FBSDKAccessToken: any;

FBSDKAccessToken.currentAccessToken().tokenString;

The only issue with this, is that the currentAccessToken() can return null in case of no token saved.

The issue I have with this, is that when I rebuild my app (like if I deleted and installed it again), then firebase correctly tells me that i'm logged in, but when I then try to access then currentAccessToken, then I get null. I really can't seem to figure out why firebase auth can say that i'm logged in, but I can't access then FB token.

if anyone could tell me why this is, then it would be much appreciated

The solution I have right now, is the following:

onAuthStateChanged: (data) => {

    if (data.loggedIn) {

        if (!FBSDKAccessToken.currentAccessToken()) {

            firebase.logout().then(_ => {
                console.log("Successfully logged out from firebase");
            }).catch(err => {
                console.log("Error logging out from firebase");
            });

        } else {

            const token = FBSDKAccessToken.currentAccessToken().tokenString;

            console.log("Logged in to firebase & facebook");
            console.log("FirebaseUser: " + JSON.stringify(data.user));
            console.log("Facebook AccessToken: " + token);
        }

    } else {

        console.log("Logged out from firebase");
    }
}

@mikkeldamm should your solution work for both iOS and Android?

Update: this seems to work fine on iOS but not on Android, where FBSDKToken is not defined. I'll have a look at it.

@abhayastudios no sadly :( .. This is a quick and dirty fix to make it work on IOS, but I would properly spend some time making a handler for this that ensures the abstraction of both (any) system beneath.

But the best solution of course, would be to have access to the current providers accessToken trough the firebase auth. The only thing that seems strange for me, is that firebase can see that i'm logged in, but facebook apparently cannot - so if someone could explain me why that is, then it would be nice :D

@mikkeldamm I just created PR #338 that should return the FB token in the provider result something like this:

"providers": [
         {
             "id": "facebook.com",
             "token": "<FB token>"
         }
     ],

Seems to work fine for me on Android and iOS simulator. Hope it holds in all cases.

@abhayastudios is there any way to get the access_token from google?

Thanks @abhayastudios

+1 to @Jai-Krish.

Any way to get access token for google login?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NNieto picture NNieto  路  4Comments

yencolon picture yencolon  路  4Comments

romandragan picture romandragan  路  3Comments

tsili852 picture tsili852  路  3Comments

thunder413 picture thunder413  路  3Comments