3
votes

I'm currently implementing a Facebook login button into my react native application. Once the user logged in successfully I grab some information, firstname, lastname and email address.

After reading the SDK documentation and online examples I can see two different approaches to achieve this:

  1. Using LoginButton & AccessToken then do a GraphRequest

  2. Using LoginManager then do a GraphRequest (the approach I chose):

    class LoginView extends Component {
        _fbAuth = () => {
            LoginManager.logInWithReadPermissions(['public_profile','email']).then(function(result){
                if(result.isCancelled){
                    console.log('loging cancelled')
                }
                else {
                    console.log('login success' + result.grantedPermissions)
    
                    const infoRequest = new GraphRequest('/me', {
                        parameters: {
                            'fields': {
                                'string' : 'email,first_name,last_name,picture'
                            }
                        }
                    }, (err, res) => {
                        console.log(err, res);
                    });
                    new GraphRequestManager().addRequest(infoRequest).start();
    
                }
            }, function(error){
                console.log('An error occured: ' + error)
            })
        }
    
        render(){
            return (
                <TouchableHighlight style={{flex:1}} onPress={() => {this._fbAuth()}}>
                    <View style={styles.container}>
                        <View style={styles.buttonContainer}>
                            <Text style={{flex:1}}>Login with Facebook</Text>
                        </View>
                    </View>
                </TouchableHighlight>
            )
        }
    }
    

What is the main difference between LoginManager & LoginButton?

Online examples of LoginButton also get the current token: AccessToken.getCurrentAccessToken().then(...)

With LoginManager I managed to get the user information without having to use AccessToken - is LoginManager calling AccessToken behind the scene?

Is there a preferred approach?

1
Check out my answer - Jitendra Modi

1 Answers

0
votes

What I understand is

When you use LoginButton & AccessToken then do a GraphRequest

So, you need to check whether your AccessToken is nil or not or your currentToken is valid or not, if you have accessToken then you can directly get data without clicking of Facebook Button or Login button. So it's mean that if you don't execute logout method or uninstall the app, you can still manage to get data with just help of your accessToken with Graph Request.

Now, when you use LoginManager then do a GraphRequest

So, This approach brings you everytime to Safari for autorization process of facebook App. LoginManager is usually everytime opening Facebook in Safari then return back to app and give data.

Hope you can understand.
Thank you