How do I sign in with Facebook in React Native in the new format?

2

I've previously done Facebook integration with React Native, and for that I have changed a few lines of code directly into the Android folder of the Application. But in the application I'm doing now I felt a lot of differences, among them the lack of the Android folder ...

I installed the react-native-fbsdk module, and I even ran the react-native link command, which returned me successful, but I can not use login. The "LoginManager.logInWithReadPermissions" call returns me a run-time error: "undefined is not an object".

I do not know the location of the android folder, I just see that the application now runs on the Expo. How do I resolve this login now?

    
asked by anonymous 22.06.2017 / 20:40

1 answer

4

After some (long) search, I was able to solve my problem, so here is the solution for future searches:

Login with Facebook is provided by api Expo, so it is necessary to install it:

npm install --save expo

After this, in the component:

import React, {Component} from 'react'
import { Text, Button } from 'native-base'
import Expo from 'expo';

export default class Component extends Component {
  render(
    <Button onPress={() => { this.facebookLogin() }}>
      <Text style={styles.btnText}>SIGN IN WITH FACEBOOK</Text>
    </Button>
  )

  async facebookLogin() {
    const ADD_ID = 'SUA_APP_ID_AQUI'
    const options = {
      permissions: ['public_profile', 'email', 'user_birthday'],
    }
    const {
      type,
      token,
    } = await Expo.Facebook.logInWithReadPermissionsAsync(ADD_ID, options)

    if (type === 'success') {
      const baseUrl = 'https://graph.facebook.com/me?access_token=${token}'
      const fields = '&fields=email,first_name,last_name,birthday'
      const response = await fetch('${baseUrl}${fields}')

      console.log(await response.json());
      this.authenticate(token);
    }
  }
}
    
27.06.2017 / 14:57