React Native

React Native Facebook login

Introduction

In the last tutorials, you built a User login/logout feature to your App using the Parse.User class. Now you will learn how to use Facebook FBSDK Login to retrieve user data from Facebook and log in, sign up or link existent users with it. You will also install and configure the react-native-fbsdk lib to achieve that.

The Parse.User.linkWith method is responsible for signing up and logging in users using any third-party authentication method, as long as you pass the right parameters requested by each different provider. After linking the user data to a new or existent Parse.User, Parse will store a valid user session in your device. Future calls to methods like currentAsync will successfully retrieve your User data, just like with regular logins.

At any time, you can access this project via our GitHub repositories to checkout the styles and complete code.

Prerequisites

To complete this tutorial, you will need:

Goal

To build a User LogIn feature using Facebook FBSDK Login on Parse for a React Native App.

Step 1 - Installing dependencies

The most popular way to add Facebook login on React Native is using react-native-fbsdk to handle it. Since this library configuration depends on your development environment, target platform, and preferences, set it up following the official docs.

Note: If you are developing for iOS, make sure that your project has support for Swift files, containing a Bridging Header. Also, pay close attention to where you add the Facebook App IDs inside your info.plist file and if your Pods files are correctly generated.

Step 2 - Using FBSDK Login with Parse

Let’s now create a new method inside the UserLogIn component calling Facebook FBSDK authentication modal with LoginManager.logInWithPermissions, requesting permission only to access the user email. If the user successfully signs in with Facebook, you can then call AccessToken.getCurrentAccessToken to retrieve the user access token from Facebook. After that, you still need to get the user id and email using a GraphRequest through FBSDK GraphRequestManager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const doUserLogInFacebook = async function () {
  try {
    // Login using the Facebook login dialog asking form email permission
    return await LoginManager.logInWithPermissions(['email']).then(
      (loginResult) => {
        if (loginResult.isCancelled) {
          console.log('Login cancelled');
          return false;
        } else {
          // Retrieve access token from FBSDK to be able to linkWith Parse
          AccessToken.getCurrentAccessToken().then((data) => {
            const facebookAccessToken = data.accessToken;
            // Callback that will be called after FBSDK successfuly retrieves user email and id from FB
            const responseEmailCallback = async (
              error,
              emailResult,
            ) => {
              if (error) {
                console.log('Error fetching data: ' + error.toString());
              } else {
                // Format authData to provide correctly for Facebook linkWith on Parse
                const facebookId = emailResult.id;
                const facebookEmail = emailResult.email;
                const authData = {
                  id: facebookId,
                  access_token: facebookAccessToken,
                };
                // Log in or sign up on Parse using this Facebook credentials
                let userToLogin = new Parse.User();
                // Set username and email to match provider email
                userToLogin.set('username', facebookEmail);
                userToLogin.set('email', facebookEmail);
                return await userToLogin
                  .linkWith('facebook', {
                    authData: authData,
                  })
                  .then(async (loggedInUser) => {
                    // logIn returns the corresponding ParseUser object
                    Alert.alert(
                      'Success!',
                      `User ${loggedInUser.get(
                        'username',
                      )} has successfully signed in!`,
                    );
                    // To verify that this is in fact the current user, currentAsync can be used
                    const currentUser = await Parse.User.currentAsync();
                    console.log(loggedInUser === currentUser);
                    // Navigation.navigate takes the user to the screen named after the one
                    // passed as parameter
                    navigation.navigate('Home');
                    return true;
                  })
                  .catch(async (error) => {
                    // Error can be caused by wrong parameters or lack of Internet connection
                    Alert.alert('Error!', error.message);
                    return false;
                  });
              }
            };

            // Formats a FBSDK GraphRequest to retrieve user email and id
            const emailRequest = new GraphRequest(
              '/me',
              {
                accessToken: facebookAccessToken,
                parameters: {
                  fields: {
                    string: 'email',
                  },
                },
              },
              responseEmailCallback,
            );

            // Start the graph request, which will call the callback after finished
            new GraphRequestManager().addRequest(emailRequest).start();

            return true;
          });
        }
      },
      (error) => {
        console.log('Login fail with error: ' + error);
        return false;
      },
    );
  } catch (error) {
    Alert.alert('Error!', error.code);
    return false;
  }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const doUserLogInFacebook = async function (): Promise<boolean> {
  try {
    // Login using the Facebook login dialog asking form email permission
    return await LoginManager.logInWithPermissions(['email']).then(
      (loginResult: object) => {
        if (loginResult.isCancelled) {
          console.log('Login cancelled');
          return false;
        } else {
          // Retrieve access token from FBSDK to be able to linkWith Parse
          AccessToken.getCurrentAccessToken().then((data: object) => {
            const facebookAccessToken = data.accessToken;
            // Callback that will be called after FBSDK successfuly retrieves user email and id from FB
            const responseEmailCallback = async (
              error: string,
              emailResult: object,
            ) => {
              if (error) {
                console.log('Error fetching data: ' + error.toString());
              } else {
                // Format authData to provide correctly for Facebook linkWith on Parse
                const facebookId: string = emailResult.id;
                const facebookEmail: string = emailResult.email;
                const authData: object = {
                  id: facebookId,
                  access_token: facebookAccessToken,
                };
                // Log in or sign up on Parse using this Facebook credentials
                let userToLogin: Parse.User = new Parse.User();
                // Set username and email to match provider email
                userToLogin.set('username', facebookEmail);
                userToLogin.set('email', facebookEmail);
                return await userToLogin
                  .linkWith('facebook', {
                    authData: authData,
                  })
                  .then(async (loggedInUser: Parse.User) => {
                    // logIn returns the corresponding ParseUser object
                    Alert.alert(
                      'Success!',
                      `User ${loggedInUser.get(
                        'username',
                      )} has successfully signed in!`,
                    );
                    // To verify that this is in fact the current user, currentAsync can be used
                    const currentUser: Parse.User = await Parse.User.currentAsync();
                    console.log(loggedInUser === currentUser);
                    // Navigation.navigate takes the user to the screen named after the one
                    // passed as parameter
                    navigation.navigate('Home');
                    return true;
                  })
                  .catch(async (error: object) => {
                    // Error can be caused by wrong parameters or lack of Internet connection
                    Alert.alert('Error!', error.message);
                    return false;
                  });
              }
            };

            // Formats a FBSDK GraphRequest to retrieve user email and id
            const emailRequest = new GraphRequest(
              '/me',
              {
                accessToken: facebookAccessToken,
                parameters: {
                  fields: {
                    string: 'email',
                  },
                },
              },
              responseEmailCallback,
            );

            // Start the graph request, which will call the callback after finished
            new GraphRequestManager().addRequest(emailRequest).start();

            return true;
          });
        }
      },
      (error: string) => {
        console.log('Login fail with error: ' + error);
        return false;
      },
    );
  } catch (error: object) {
    Alert.alert('Error!', error.code);
    return false;
  }
};

Note that after the GraphRequest succeeds, this function uses Parse.User.linkWith on a new Parse.User object to register a new user or log in a previous one with these credentials, passing his Facebook authentication data accordingly.

Add this function to your UserSignIn component and assign it to your Facebook button onPress parameter. Go ahead and test your new function, you will see that the user will be redirected to your home screen after successfully signing in.

React Native Back4App

Step 3 - Verifying user sign in and session creation

To make sure that the Facebook sign-in worked, you can look at your Parse dashboard and see your new User (if your Facebook authentication data didn’t belong to another user), containing the Facebook authData parameters.

React Native Back4App

You can also verify that a valid session was created in the dashboard, containing a pointer to that User object.

React Native Back4App

Step 4 - Linking an existing User to FBSDK Login

Another linkWith possible use is to link an existing user with another auth provider, in this case, Facebook. Add this function that calls linkWith the same way you did in UserLogIn to your HelloUser component or directly to your home screen. The only difference here is that instead of calling the method from an empty Parse.User, you will use it from the logged-in user object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const doUserLinkFacebook = async function () {
  try {
    // Login using the Facebook login dialog asking form email permission
    return await LoginManager.logInWithPermissions(['email']).then(
      (loginResult) => {
        if (loginResult.isCancelled) {
          console.log('Login cancelled');
          return false;
        } else {
          // Retrieve access token from FBSDK to be able to linkWith Parse
          AccessToken.getCurrentAccessToken().then((data) => {
            const facebookAccessToken = data.accessToken;
            // Callback that will be called after FBSDK successfuly retrieves user email and id from FB
            const responseEmailCallback = async (
              error,
              emailResult,
            ) => {
              if (error) {
                console.log('Error fetching data: ' + error.toString());
              } else {
                // Format authData to provide correctly for Facebook linkWith on Parse
                const facebookId = emailResult.id;
                const authData = {
                  id: facebookId,
                  access_token: facebookAccessToken,
                };
                let currentUser = await Parse.User.currentAsync();
                return await currentUser
                  .linkWith('facebook', {
                    authData: authData,
                  })
                  .then(async (loggedInUser) => {
                    // logIn returns the corresponding ParseUser object
                    Alert.alert(
                      'Success!',
                      `User ${loggedInUser.get(
                        'username',
                      )} has successfully linked his Facebook account!`,
                    );
                    // To verify that this is in fact the current user, currentAsync can be used
                    currentUser = await Parse.User.currentAsync();
                    console.log(loggedInUser === currentUser);
                    return true;
                  })
                  .catch(async (linkWithError) => {
                    // Error can be caused by wrong parameters or lack of Internet connection
                    Alert.alert('Error!', linkWithError.message);
                    return false;
                  });
              }
            };

            // Formats a FBSDK GraphRequest to retrieve user email and id
            const emailRequest = new GraphRequest(
              '/me',
              {
                accessToken: facebookAccessToken,
                parameters: {
                  fields: {
                    string: 'email',
                  },
                },
              },
              responseEmailCallback,
            );

            // Start the graph request, which will call the callback after finished
            new GraphRequestManager().addRequest(emailRequest).start();

            return true;
          });
        }
      },
      (error) => {
        console.log('Login fail with error: ' + error);
        return false;
      },
    );
  } catch (error) {
    Alert.alert('Error!', error.code);
    return false;
  }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const doUserLinkFacebook = async function (): Promise<boolean> {
  try {
    // Login using the Facebook login dialog asking form email permission
    return await LoginManager.logInWithPermissions(['email']).then(
      (loginResult: object) => {
        if (loginResult.isCancelled) {
          console.log('Login cancelled');
          return false;
        } else {
          // Retrieve access token from FBSDK to be able to linkWith Parse
          AccessToken.getCurrentAccessToken().then((data: object) => {
            const facebookAccessToken = data.accessToken;
            // Callback that will be called after FBSDK successfuly retrieves user email and id from FB
            const responseEmailCallback = async (
              error: string,
              emailResult: object,
            ) => {
              if (error) {
                console.log('Error fetching data: ' + error.toString());
              } else {
                // Format authData to provide correctly for Facebook linkWith on Parse
                const facebookId: string = emailResult.id;
                const authData: object = {
                  id: facebookId,
                  access_token: facebookAccessToken,
                };
                let currentUser: Parse.User = await Parse.User.currentAsync();
                return await currentUser
                  .linkWith('facebook', {
                    authData: authData,
                  })
                  .then(async (loggedInUser: Parse.User) => {
                    // logIn returns the corresponding ParseUser object
                    Alert.alert(
                      'Success!',
                      `User ${loggedInUser.get(
                        'username',
                      )} has successfully linked his Facebook account!`,
                    );
                    // To verify that this is in fact the current user, currentAsync can be used
                    currentUser = await Parse.User.currentAsync();
                    console.log(loggedInUser === currentUser);
                    return true;
                  })
                  .catch(async (linkWithError: object) => {
                    // Error can be caused by wrong parameters or lack of Internet connection
                    Alert.alert('Error!', linkWithError.message);
                    return false;
                  });
              }
            };

            // Formats a FBSDK GraphRequest to retrieve user email and id
            const emailRequest = new GraphRequest(
              '/me',
              {
                accessToken: facebookAccessToken,
                parameters: {
                  fields: {
                    string: 'email',
                  },
                },
              },
              responseEmailCallback,
            );

            // Start the graph request, which will call the callback after finished
            new GraphRequestManager().addRequest(emailRequest).start();

            return true;
          });
        }
      },
      (error: string) => {
        console.log('Login fail with error: ' + error);
        return false;
      },
    );
  } catch (error: object) {
    Alert.alert('Error!', error.code);
    return false;
  }
};

Assign this function to a Facebook button onPress parameter on your home screen. Test your new function, noting that the Parse.User object authData value will be updated with the new auth provider data. Verify if the user has indeed updated in your Parse server dashboard.

React Native Back4App

Conclusion

At the end of this guide, you learned how to log in or sign up Parse users on React Native using Facebook FBSDK Login with react-native-fbsdk. In the next guide, we will show you how to perform useful user queries.