Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the user is already logged in?

I am complete new to react-native and Facebook sdk

So I have followed the tutorial given here and everything works fine, the user is able to log in using facebook.

My question is :

How do I check if the user is already logged in when he opens the app next time?

I thought the callback onLoginFinished is called everytime regardless whether the user has already logged in or not. But that is not the case. If the user is logged in, the onLoginFinished not called. Is there any perticular callback to check that? or any other method or some workaround?

I tried to read the docs of react-native-fbsdk here but it seems they are not enough.

Here is the Login button:

<LoginButton
    onLoginFinished={ (error, result) => { this.handleLogIn(error, result) } }
    onLogoutFinished={() => { this.handleLogOut() } } 
/>

And the helper functions:

handleLogIn(error, result) {
    if (error) {
        alert("Login failed with error: " + result.error);
    } else if (result.isCancelled) {
        alert("Login was cancelled");
    } else {
        alert("Login was successful")
        AccessToken.getCurrentAccessToken().then(
            (data) => {console.log(data);} //Refresh it every time
        );
    }
}

handleLogOut() {
    console.log("User logged out");
    alert("Log out successful");
}
like image 401
Anand Undavia Avatar asked Dec 05 '22 15:12

Anand Undavia


1 Answers

You need to check if currentAccessToken is valid in the beginning somewhere appropriate when your app is started. If it returns null, you need to refresh the token and if refresh token is expired, redirect user to loginpage.

AccessToken.getCurrentAccessToken().then(
            (data) => {console.log(data);} //Refresh it every time
        );
like image 151
agenthunt Avatar answered Jan 26 '23 22:01

agenthunt