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 Login to retrieve user data from Facebook and log in, sign up or link existent users with it. You will also install and configure react-facebook-login 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 on your device. Future calls to methods like current will successfully retrieve your User data, just like with regular logins.
If you want to test/use the screen layout provided by this guide, you should set up the Ant Design library.
Goal
To build a User LogIn feature using Facebook Login on Parse for a React App.
1 - Installing dependencies
The most popular way to enable Facebook Login on React is using react-facebook-login to handle it. Set it up following the official docs.
To use the lib component, you need to inform a valid Facebook app appID, so create one on on your Facebook for Developers dashboard, following one of their official guides. After that, add Facebook Login capability to your app while choosing the WWW option, informing https://localhost:3000 as your website URL.
Facebook Login requires that your connection is secure, even when logging in on a development environment. So, you need to setup HTTPS access for your React app by creating a local certificate, giving it full permissions in your OS and adding the files to your project. Since each OS can have its particularities, this won’t be covered in the guide.
After creating the certificate, make sure to change in the scripts session of your package.json file the start command to:
If you are using Typescript, you need to create a new file in your src folder called react-facebook-login.d.ts containing the react-facebook-login module declaration to use the styleless version of the component:
Let’s now create a new method inside the UserLogIn component that will handle the response when calling a Facebook Login authentication modal. If the user signs in with Facebook, this call will retrieve the user data from Facebook and you need to store the id, accessToken, and the profile email. Then, the function will try to log in on Parse using the Parse.User.linkWith method and these credentials. Note that if your user had already signed up using this Facebook authentication, linkWith will log him in using the existent account.
JavaScript
TypeScript
1consthandleFacebookLoginLoginResponse=asyncfunction(response:any):Promise<boolean>{2// Check if response has an error3if(response.error !==undefined){4console.log(`Error: ${response.error}`);5returnfalse;6}else{7try{8// Gather Facebook user info9const userId:string= response.id;10const userEmail:string= response.email;11const userAccessToken:string= response.accessToken;12// Try to login on Parse using linkWith and these credentials13// Create a new Parse.User object14const userToLogin: Parse.User =newParse.User();15// Set username and email to match facebook profile email16 userToLogin.set('username', userEmail);17 userToLogin.set('email', userEmail);18try{19let loggedInUser: Parse.User =await userToLogin
20.linkWith('facebook',{21 authData:{id: userId, access_token: userAccessToken},22});23// logIn returns the corresponding ParseUser object24alert(25`Success! User ${loggedInUser.get('username')} has successfully signed in!`,26);27// Update state variable holding current user28getCurrentUser();29returntrue;30}catch(error:any){31// Error can be caused by wrong parameters or lack of Internet connection32alert(`Error! ${error.message}`);33returnfalse;34}35}catch(error:any){36console.log("Error gathering Facebook user info, please try again!")37returnfalse;38}39}40}
After that, you need to use the react-facebook-loginFacebookLogin component to call the Facebook Login modal, adding it to your JSX code. You can use Facebook’s default styling or create a custom one, which is the way followed by this guide. Here is the full UserLogin component code, note the react-facebook-login button and how it is tied to the modal response method created before.
JavaScript
TypeScript
1import React,{ useState,FC, ReactElement }from'react';2import'./App.css';3import{ Button, Divider, Input }from'antd';4import FacebookLogin from'react-facebook-login/dist/facebook-login-render-props';5const Parse =require('parse/dist/parse.min.js');67exportconst UserLogin:FC<{}>=(): ReactElement =>{8// State variables9const[username, setUsername]=useState('');10const[password, setPassword]=useState('');11const[currentUser, setCurrentUser]=useState<Parse.Object |null>(null);1213constdoUserLogIn=asyncfunction():Promise<boolean>{14// Note that these values come from state variables that we've declared before15const usernameValue:string= username;16const passwordValue:string= password;17try{18const loggedInUser: Parse.User =await Parse.User.logIn(usernameValue, passwordValue);19// logIn returns the corresponding ParseUser object20alert(21`Success! User ${loggedInUser.get('username')} has successfully signed in!`,22);23// To verify that this is in fact the current user, `current` can be used24const currentUser: Parse.User =await Parse.User.current();25console.log(loggedInUser === currentUser);26// Clear input fields27setUsername('');28setPassword('');29// Update state variable holding current user30getCurrentUser();31returntrue;32}catch(error:any){33// Error can be caused by wrong parameters or lack of Internet connection34alert(`Error! ${error.message}`);35returnfalse;36}37};3839constdoUserLogOut=asyncfunction():Promise<boolean>{40try{41await Parse.User.logOut();42// To verify that current user is now empty, currentAsync can be used43const currentUser: Parse.User =await Parse.User.current();44if(currentUser ===null){45alert('Success! No user is logged in anymore!');46}47// Update state variable holding current user48getCurrentUser();49returntrue;50}catch(error:any){51alert(`Error! ${error.message}`);52returnfalse;53}54};5556// Function that will return current user and also update current username57constgetCurrentUser=asyncfunction():Promise<Parse.User |null>{58const currentUser:(Parse.User |null)=await Parse.User.current();59// Update state variable holding current user60setCurrentUser(currentUser);61return currentUser;62}6364consthandleFacebookLoginLoginResponse=asyncfunction(response:any):Promise<boolean>{65// Check if response has an error66if(response.error !==undefined){67console.log(`Error: ${response.error}`);68returnfalse;69}else{70try{71// Gather Facebook user info72const userId:string= response.id;73const userEmail:string= response.email;74const userAccessToken:string= response.accessToken;75// Try to login on Parse using linkWith and these credentials76// Create a new Parse.User object77const userToLogin: Parse.User =newParse.User();78// Set username and email to match facebook profile email79 userToLogin.set('username', userEmail);80 userToLogin.set('email', userEmail);81try{82let loggedInUser: Parse.User =await userToLogin
83.linkWith('facebook',{84 authData:{id: userId, access_token: userAccessToken},85});86// logIn returns the corresponding ParseUser object87alert(88`Success! User ${loggedInUser.get('username')} has successfully signed in!`,89);90// Update state variable holding current user91getCurrentUser();92returntrue;93}catch(error:any){94// Error can be caused by wrong parameters or lack of Internet connection95alert(`Error! ${error.message}`);96returnfalse;97}98}catch(error:any){99console.log("Error gathering Facebook user info, please try again!")100returnfalse;101}102}103}104105return(106<div>107<div className="header">108<img
109 className="header_logo"110 alt="Back4App Logo"111 src={112'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'113}114/>115<p className="header_text_bold">{'React on Back4App'}</p>116<p className="header_text">{'User Login'}</p>117</div>118{currentUser ===null&&(119<div className="container">120<h2 className="heading">{'User Login'}</h2>121<Divider />122<div className="form_wrapper">123<Input
124 value={username}125 onChange={(event)=>setUsername(event.target.value)}126 placeholder="Username"127 size="large"128 className="form_input"129/>130<Input
131 value={password}132 onChange={(event)=>setPassword(event.target.value)}133 placeholder="Password"134 size="large"135 type="password"136 className="form_input"137/>138</div>139<div className="form_buttons">140<Button
141 onClick={()=>doUserLogIn()}142 type="primary"143 className="form_button"144 color={'#208AEC'}145 size="large"146 block
147>148 Log In
149</Button>150</div>151<Divider />152<div className="login-social">153<FacebookLogin
154 appId="4573670722644997"155 fields="email"156 callback={handleFacebookLoginLoginResponse}157 render={(renderProps:{onClick:()=>void})=>(158<div className="login-social-item login-social-item--facebook">159<img onClick={renderProps.onClick} className="login-social-item__image" src={'https://findicons.com/files/icons/2830/clean_social_icons/250/facebook.png'} alt=""/>160</div>161)}162/>163<div className="login-social-item">164<img className="login-social-item__image" src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN8AAADiCAMAAAD5w+JtAAABWVBMVEX////qQzU0qFNChfT7vAUufPPk7f08gvR0o/Zzofb7uQD7uADpNCP/vQDqPzAspk7pLhrpOysYokLpOyzpNST97OvwgnskpEnsXFH8wgAVoUHpLRj+9/b0paD4xsOIx5fd7+H74uDvenLrU0f61tT1r6vuc2vtZVvxioP5zsvpNjb93p4nefOFrPeqxPnK5dBUs2zt9u++0vtuvYFelfWq1rT2u7j86ejyl5HrSz7/9+X80nT94637xDX8yU//+/D93Jb+785SjvWdu/j+9NzC1fvv9P7a5v1FrmDl8+nD4spru34zqkLoHwD0qKTwhnPwdjf1ly/5sSL82IbuZjjyiDL3pyfsWDjwezb1mi7vazn8zWH+68L7wjDq04OkszlurkrfuiG7tjKGsERSq1DSuSqatEQ4o31Kk9pJnrQ/qmRIjuVJmcRHo5uYzqVKmMtIoqJBqHlesJcm1X3QAAALTUlEQVR4nO2c2X/a2BWAFRniBAttyIiwmNUsM46BEGycZDY7cTC47iztdKbJdJ1u09ad9v9/qCQESKDlbrpX4pfvyS9Y+nzOvefcBXPcB0hRLh+en9cNzg/LrN+FHIeto+Nuo9PMlQzkBeZPYqHRrZz1zpOrWm4ddwuiLAuakhPFB5uIuZyiCbLSbFSODlm/KySGWv6iZIhta3l4KkIp1670khLJVqWjyVoOQM2BIak1J7F3LB81NBkkap6RNALZPo5vrpbP2oKQQ3NbOWpyIZ6KvQa23FKx1DmLWaK2JgoZO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I4/DuoiRor5o6qI/pdxx69MaRT3OTFKKhjqD76QPq0VKSSoVq7S/jWdxQ2UYSuSufUFTG0UdQ6k4qrGyMzFiGOE4NGIXfUEPM7zXI6qHulplbmcxHpAfiJLK37P2WlOrSiQVJV2dM/iKfSBb96uQ0YvTMbMpM4jZHHsomheC7musRfyZjXT0MEp6cTCOx5QSQO1+gOBoBI6vzmKZltsMa+PRFOT2lWVmqBWnVYCbePFi2B9XB7x5G0vy9LSubBndwaA6ZvMPgYgwrM3G1dFgKqnForrC+Jm3rtzVEpKRIAyHNzc1i5sdsmLK/wHcjdWqyATPYAAAAABJRU5ErkJggg=='} alt=""/>165</div>166<div className="login-social-item">167<img className="login-social-item__image" src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAV1BMVEWZmZn///+SkpKVlZWRkZH19fWgoKDg4OCampqjo6P8/Py+vr7l5eX29vb5+fnIyMirq6vQ0NDp6enW1ta3t7fU1NS7u7uwsLDKysrv7+/b29vi4uKtra2OjCyUAAAJGUlEQVR4nO2d6ZrqIAyGKdSldrSrVh3v/zpPq6PjjCULxhLnOd9/aV8LAUISTKJSq8OxaEtzFGjKCLQhrboojXXOGLcVaE0dYZ33dOYitxdoUBnhNrvhDYSFQJOaCFd7c4f39whXhfvJ99cIG/ObryesBBpWQjjL7APfn7Kl1RifMfavzIebchzQ2FqgdQWE9cgI/CKcCTQfn/CYevh6SbQfnXAPAJYSD4hNWACALpd4QmTC3GNjLsNQwpRGJiwgQJNKGJq4hBXQRXvNRR4Sk/ATBpQZhjEJD2AX7Yfhh8hjIhLOYUDjZB4TjzD3rWRkO2k8wgU8CHtLKrEoTeIRrhA+YzKhJ8UibJE+amwj9KRIhB9YH5VZdQ+KRIjZURkHxllxCLfIVNhrJfWsOITYIJQbhZEIK5RQZkl6VhRCtIuKOGiuD5Nriix0FLpW8GkxCFFDKmdmkiiEH9gnTD8lHxeBcDfRkvtLEQixLiq1IL0+TrY5gj6R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knxqGIuEe4HDCZB9/Ce5K+uuGVF6Kf6kl5rl4ljC6taEPwlDCTdyJP2VVHQgijJtuYnn56mGESR0PkbLrFSBMPmLNGY5bNiKUMNbdAo54J6AAYaTNIu1WRxnCpJ6ej3YvpxhhMltyp35nbWrKrmtP2TJNLfvnAYBPESZJyXhHZ+fdfnEXfbb+qDrDobTcS3QECJOcNhiddV0zmqFU7zMqJJYc49GThMnRoe/n7DKHfEazKksJkCE1Ewc9S5isS3DacNbscM/7oVgiX9KWAeXYz3qaMEka4305Z1tqCbnPFmB0vhBnggQIk1U++nLOlg1nel415Tikc0VA7dmrJAj7rpq7X33VpVnFvzFltu3sL8reSOWhHfQsGcJex1P/Lu7yl1vTBeB9qd6fjO05B1k7z1nXOY5IjLDvZfU2b09lVzQB1X5/alYvmmpfHT+C/6dv/QMH/ovCU90cLAAAAABJRU5ErkJggg=='} alt=""/>168</div>169</div>170<p className="form__hint">Don't have an account?<a className="form__link" href="#">Sign up</a></p>171</div>172)}173{currentUser !==null&&(174<div className="container">175<h2 className="heading">{'User Screen'}</h2>176<Divider />177<h2 className="heading">{`Hello ${currentUser.get('username')}!`}</h2>178<div className="form_buttons">179<Button
180 onClick={()=>doUserLogOut()}181 type="primary"182 className="form_button"183 color={'#208AEC'}184 size="large"185 block
186>187 Log Out
188</Button>189</div>190</div>191)}192</div>193);194};
Add these classes to your App.css file if you want to fully render this component’s layout.
Go ahead and test your new function. If you were able to sign in to Facebook and the Parse linkWith call was successful, you should see a success message like this.
3 - Verifying user sign in and session creation
To make sure that the Facebook Login 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.
You can also verify that a valid session was created in the dashboard, containing a pointer to that User object.
4 - Linking an existing User to Facebook Sign-in
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 as logging in to your UserLogIn component. The only difference here is that instead of calling the method from an empty Parse.User, you will use it from the currently logged-in user object.
JavaScript
TypeScript
1consthandleFacebookLoginLinkResponse=asyncfunction(response:any):Promise<boolean>{2// Check if response has an error3if(response.error !==undefined){4console.log(`Error: ${response.error}`);5returnfalse;6}else{7try{8// Gather Facebook user info9const userId:string= response.id;10const userAccessToken:string= response.accessToken;11// Try to link current Parse user using linkWith and these credentials12// Get current user13const userToLink: Parse.User =await Parse.User.current();14try{15let loggedInUser: Parse.User =await userToLink
16.linkWith('facebook',{17 authData:{id: userId, access_token: userAccessToken},18});19// logIn returns the corresponding ParseUser object20alert(21`Success! User ${loggedInUser.get(22'username',23)} has successfully linked his Facebook account!`,24);25// Update state variable holding current user26getCurrentUser();27returntrue;28}catch(error:any){29// Error can be caused by wrong parameters or lack of Internet connection30alert(`Error! ${error.message}`);31returnfalse;32}33}catch(error:any){34console.log("Error gathering Facebook user info, please try again!")35returnfalse;36}37}38}
Assign this function to another react-facebook-login component in your home screen, which is shown only when there is a current user logged in in your app. 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.
Conclusion
At the end of this guide, you learned how to log in, sign up or link existing Parse users on React using Facebook Login with react-google-login. In the next guide, we will show you how to use Apple sign-in.