Login every time when you click on facebook button on ios

I have made a login with facebook button, when I click on that button it asks me for credentials and after successful login, it will alert my name and if I click it next time it will not ask me for credentials and only alert my name but this process only works on the android device. when I follow this process on IOS it asks me for credentials every time I click on the button.

async logIn() {
  try {
    const {
      type,
      token,
      expires,
      behavior,
      permissions,
      declinedPermissions,
    } = await Facebook.logInWithReadPermissionsAsync('<My_app_id>', {
      permissions: ['public_profile'],
      behavior: Platform.OS == "ios" ? Expo.Constants.statusBarHeight < 40 ? "web" : "native" : "native",
      //behaviour:'system',
    });
    if (type === 'success') {
      // Get the user's name using Facebook's Graph API
      const response = await fetch(`https://graph.facebook.com/me?access_token=${token}`);
      Alert.alert('Logged in!', `Hi ${(await response.json()).name}!`);
    } else {
      // type === 'cancel'
    }
  } catch ({ message }) {
    alert(`Facebook Login Error: ${message}`);
  }
}

<View style={styles.container}>
      <TouchableOpacity style={{justifyContent:'center',alignItems:'center',flex:1,}}>
        <Button color="#4267B2"
                onPress={this.logIn.bind(this)}
                title='connect with facebook'/>
      </TouchableOpacity>
</View>

This is the code of the following approach.
Expected behaviour: I want it to Keep me logged in every time just like it is doing on android

Could you please clarify a little more. Are you looking to have the login persist each time you open the app? Or do you want to have to click the login button every time the app opens, but just not have to login to facebook through the pop up?

Thanks for reply, I want when a login button click for the first from new device it ask for the credentials and if same user click the login button again then it will only alert the name of the user i.e. it does not ask the user credentials again. I am having this issue in IOS only.
Basically I want it to keep me logged in

I believe that is more so the behavior of the operating system than expo. The only thing I’ve been able to do in the past is to save the auth token that facebook gives you and automatically sign back in using it.

Can you please provide the code for this approach

So in your code, after you login, with facebook, you get a token back. I then use Expo’s SecureStore, to store that in the phone which persists between each time you open the app again. When the app first loads, I grab that token, make sure the token is still valid, and then I hit the facebook graph directly with that token to get the user info instead of having them log back in. I can try and put together an example if this isn’t clear enough to get you started.

This is the approach I have tried:

async logIn() {
  try {
   if( this.state.response == ""){ 
    const {
         type,
         token,
         expires,
         permissions,
         declinedPermissions,
       } = await Facebook.logInWithReadPermissionsAsync('<App_Id>', {
         permissions: ['public_profile'],
         behavior: Platform.OS == "ios" ? Expo.Constants.statusBarHeight < 40 ? "web" : "native" : "native",
       });
       if (type === 'success') {
         // Get the user's name using Facebook's Graph API
         //this.setState({response : await fetch(`https://graph.facebook.com/me?access_token=${token}`)});
         const response = await fetch(`https://graph.facebook.com/me?access_token=${token}`);
         //Alert.alert('Logged in!', `Hi ${(await response.json()).name}!`);
         this.setState({response:(await response.json()).name});
         Alert.alert('Logged in!', `Hi ${this.state.response}!`);
         //await SecureStore.setItemAsync('token', token);
         //this.setState({value: await SecureStore.getItemAsync('token')});
       } else {
         if(type === 'cancel'){
           alert(`Facebook Login Cancel`);
         }
       }
     }
       else{
        Alert.alert('Logged in!', `Hi ${this.state.response}!`);
       }
  } catch ({ message }) {
    alert(`Facebook Login Error: ${message}`);
  }
}

It is working fine in android but in IOS it only keeps me logged in until I restart the app, Once I restart the app I have to give user credentials to it again.

I have been using secure store in wrong manner but now it is working i have used secure store with onload.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.