0

I am trying to set up a login screen in order to offer the user to log in via facebook via expo-facebook. I want to retrieve the name and email of the user in a console.log, to test. However, I have nothing in the terminal. Can you tell me why? However, the connection is working fine with my account. Merci pour toute aide.

logInViaFacebook = async () => {
    try {
      await Facebook.initializeAsync("34904");
      const {
        type,
        token,
        expires,
        permissions,
        declinedPermissions,
      } = await Facebook.logInWithReadPermissionsAsync({
        permissions: ["public_profile", "email"],
      });
      if (type === "success") {
        // Get the user's name using Facebook's Graph API
        const response = await fetch(
          `https://graph.facebook.com/me?fields=id,name,email,birthday&access_token=${token}`
        );
        this.setState({
            signedIn: true,
            name: response.name,
            email: response.email,
          });
          console.log(this.state.name, this.state.email,)
          this.props.navigation.navigate("TabBar"); 
        // saving the token in AsyncStorage
        //await AsyncStorage.setItem('email', email); // <- save the email in AsyncStorage for later use
      } else {
        // type === 'cancel'
      }
    } catch ({ message }) {
      alert(`Facebook Login Error: ${message}`);
  }
}
Kimako
  • 615
  • 2
  • 11
  • 26

1 Answers1

2

setState is asynchronous so you can't see update immediately. To see update, you need to pass callback as second parameter of setState.

this.setState({
   signedIn: true,
   name: response.name,
   email: response.email,
}, () => {
   console.log(this.state.name, this.state.email);
});
          
Prime
  • 2,809
  • 1
  • 7
  • 23