Search code examples
facebook-logincreate-react-native-app

How can I use facebook login with Create-react-native-app


I try to find a way to use Facebook login with Create-react-native-app for both Android and IOS. Is that possible? All this seem pretty new...


Solution

  • Use the Expo SDK. This way you won't have to eject from create-react-native-app.

    import { Facebook } from 'expo';
    
    async function logIn() {
      const { type, token } = await Facebook.logInWithReadPermissionsAsync('<APP_ID>', {
        permissions: ['public_profile'],
      });
    
      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}!`,
        );
      }
    }
    

    Snippet taken from the link.