React 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 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.
Prerequisites
To complete this tutorial, you will need:
- A React App created and connected to Back4App.
- Complete the previous guide so you can have a better understanding of the Parse.User class and the Parse.User.logIn method.
- 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.
Step 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:
package.json
1
"start": "export HTTPS=true&&SSL_CRT_FILE=cert.pem&&SSL_KEY_FILE=key.pem react-scripts start",
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:
react-facebook-login.d.ts
1
declare module 'react-facebook-login/dist/facebook-login-render-props';
Step 2 - Using Facebook Login with Parse
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.
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
const handleFacebookLoginLoginResponse = async function(response) {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId = response.id;
const userEmail = response.email;
const userAccessToken = response.accessToken;
// Try to login on Parse using linkWith and these credentials
// Create a new Parse.User object
const userToLogin = new Parse.User();
// Set username and email to match facebook profile email
userToLogin.set('username', userEmail);
userToLogin.set('email', userEmail);
try {
let loggedInUser = await userToLogin
.linkWith('facebook', {
authData: {id: userId, access_token: userAccessToken},
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get('username')} has successfully signed in!`,
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error) {
console.log("Error gathering Facebook user info, please try again!")
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
const handleFacebookLoginLoginResponse = async function(response: any): Promise<boolean> {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId: string = response.id;
const userEmail: string = response.email;
const userAccessToken: string = response.accessToken;
// Try to login on Parse using linkWith and these credentials
// Create a new Parse.User object
const userToLogin: Parse.User = new Parse.User();
// Set username and email to match facebook profile email
userToLogin.set('username', userEmail);
userToLogin.set('email', userEmail);
try {
let loggedInUser: Parse.User = await userToLogin
.linkWith('facebook', {
authData: {id: userId, access_token: userAccessToken},
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get('username')} has successfully signed in!`,
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error: any) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error: any) {
console.log("Error gathering Facebook user info, please try again!")
return false;
}
}
}
After that, you need to use the react-facebook-login
FacebookLogin
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.
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { useState } from 'react';
import Parse from 'parse/dist/parse.min.js';
import FacebookLogin from 'react-facebook-login/dist/facebook-login-render-props';
import './App.css';
import { Button, Divider, Input } from 'antd';
export const UserLogin = () => {
// State variables
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [currentUser, setCurrentUser] = useState(null);
const doUserLogIn = async function () {
// Note that these values come from state variables that we've declared before
const usernameValue = username;
const passwordValue = password;
try {
const loggedInUser = await Parse.User.logIn(usernameValue, passwordValue);
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get(
'username'
)} has successfully signed in!`
);
// To verify that this is in fact the current user, `current` can be used
const currentUser = await Parse.User.current();
console.log(loggedInUser === currentUser);
// Clear input fields
setUsername('');
setPassword('');
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
};
const doUserLogOut = async function () {
try {
await Parse.User.logOut();
// To verify that current user is now empty, currentAsync can be used
const currentUser = await Parse.User.current();
if (currentUser === null) {
alert('Success! No user is logged in anymore!');
}
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error) {
alert(`Error! ${error.message}`);
return false;
}
};
// Function that will return current user and also update current username
const getCurrentUser = async function () {
const currentUser = await Parse.User.current();
// Update state variable holding current user
setCurrentUser(currentUser);
return currentUser;
};
const handleFacebookLoginLoginResponse = async function (response) {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId = response.id;
const userEmail = response.email;
const userAccessToken = response.accessToken;
// Try to login on Parse using linkWith and these credentials
// Create a new Parse.User object
const userToLogin = new Parse.User();
// Set username and email to match facebook profile email
userToLogin.set('username', userEmail);
userToLogin.set('email', userEmail);
try {
let loggedInUser = await userToLogin.linkWith('facebook', {
authData: { id: userId, access_token: userAccessToken },
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get(
'username'
)} has successfully signed in!`
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error) {
console.log('Error gathering Facebook user info, please try again!');
return false;
}
}
};
return (
<div>
<div className="header">
<img
className="header_logo"
alt="Back4App Logo"
src={
'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
}
/>
<p className="header_text_bold">{'React on Back4App'}</p>
<p className="header_text">{'User Login'}</p>
</div>
{currentUser === null && (
<div className="container">
<h2 className="heading">{'User Login'}</h2>
<Divider />
<div className="form_wrapper">
<Input
value={username}
onChange={(event) => setUsername(event.target.value)}
placeholder="Username"
size="large"
className="form_input"
/>
<Input
value={password}
onChange={(event) => setPassword(event.target.value)}
placeholder="Password"
size="large"
type="password"
className="form_input"
/>
</div>
<div className="form_buttons">
<Button
onClick={() => doUserLogIn()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
block
>
Log In
</Button>
</div>
<Divider />
<div className="login-social">
<FacebookLogin
appId="4573670722644997"
fields="email"
callback={handleFacebookLoginLoginResponse}
render={(renderProps) => (
<div className="login-social-item login-social-item--facebook">
<img onClick={renderProps.onClick} className="login-social-item__image" src={'https://findicons.com/files/icons/2830/clean_social_icons/250/facebook.png'} alt=""/>
</div>
)}
/>
<div className="login-social-item">
<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/KySGWv6iZIhta3l4KkIp1670khLJVqWjyVoOQM2BIak1J7F3LB81NBkkap6RNALZPo5vrpbP2oKQQ3NbOWpyIZ6KvQa23FKx1DmLWaK2JgoZOVtRkPMt1k5rjguyQk5ugSI3z1h7WRxOZA1xQglGFLQK8zQ975IP3R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WdxQ2UYSuSufUFTG0UdQ6k4qrGyMzFiGOE4NGIXfUEPM7zXI6qHulplbmcxHpAfiJLK37P2WlOrSiQVJV2dM/iKfSBb96uQ0YvTMbMpM4jZHHsomheC7musRfyZjXT0MEp6cTCOx5QSQO1+gOBoBI6vzmKZltsMa+PRFOT2lWVmqBWnVYCbePFi2B9XB7x5G0vy9LSubBndwaA6ZvMPgYgwrM3G1dFgKqnForrC+Jm3rtzVEpKRIAyHNzc1i5sdsmLK/wHcjdWqyATPYAAAAABJRU5ErkJggg=='} alt=""/>
</div>
<div className="login-social-item">
<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+TrY5gj6RTurWss+bnhDZF0pOFGdNT7gEAVMRD+K9JiecwQ5EsQX3TZMTHiHC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dyJP2VVHQgijJtuYnn56mGESR0PkbLrFSBMPmLNGY5bNiKUMNbdAo54J6AAYaTNIu1WRxnCpJ6ej3YvpxhhMltyp35nbWrKrmtP2TJNLfvnAYBPESZJyXhHZ+fdfnEXfbb+qDrDobTcS3QECJOcNhiddV0zmqFU7zMqJJYc49GThMnRoe/n7DKHfEazKksJkCE1Ewc9S5isS3DacNbscM/7oVgiX9KWAeXYz3qaMEka4305Z1tqCbnPFmB0vhBnggQIk1U++nLOlg1nel415Tikc0VA7dmrJAj7rpq7X33VpVnFvzFltu3sL8reSOWhHfQsGcJex1P/Lu7yl1vTBeB9qd6fjO05B1k7z1nXOY5IjLDvZfU2b09lVzQB1X5/alYvmmpfHT+C/6dv/QMH/ovCU90cLAAAAABJRU5ErkJggg=='} alt=""/>
</div>
</div>
<p className="form__hint">Don't have an account? <a className="form__link" href="#">Sign up</a></p>
</div>
)}
{currentUser !== null && (
<div className="container">
<h2 className="heading">{'User Screen'}</h2>
<Divider />
<h2 className="heading">{`Hello ${currentUser.get('username')}!`}</h2>
<div className="form_buttons">
<Button
onClick={() => doUserLogOut()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
block
>
Log Out
</Button>
</div>
</div>
)}
</div>
);
};
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, { useState, FC, ReactElement } from 'react';
import './App.css';
import { Button, Divider, Input } from 'antd';
import FacebookLogin from 'react-facebook-login/dist/facebook-login-render-props';
const Parse = require('parse/dist/parse.min.js');
export const UserLogin: FC<{}> = (): ReactElement => {
// State variables
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [currentUser, setCurrentUser] = useState<Parse.Object | null>(null);
const doUserLogIn = async function (): Promise<boolean> {
// Note that these values come from state variables that we've declared before
const usernameValue: string = username;
const passwordValue: string = password;
try {
const loggedInUser: Parse.User = await Parse.User.logIn(usernameValue, passwordValue);
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get('username')} has successfully signed in!`,
);
// To verify that this is in fact the current user, `current` can be used
const currentUser: Parse.User = await Parse.User.current();
console.log(loggedInUser === currentUser);
// Clear input fields
setUsername('');
setPassword('');
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error: any) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
};
const doUserLogOut = async function (): Promise<boolean> {
try {
await Parse.User.logOut();
// To verify that current user is now empty, currentAsync can be used
const currentUser: Parse.User = await Parse.User.current();
if (currentUser === null) {
alert('Success! No user is logged in anymore!');
}
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error: any) {
alert(`Error! ${error.message}`);
return false;
}
};
// Function that will return current user and also update current username
const getCurrentUser = async function (): Promise<Parse.User | null> {
const currentUser: (Parse.User | null) = await Parse.User.current();
// Update state variable holding current user
setCurrentUser(currentUser);
return currentUser;
}
const handleFacebookLoginLoginResponse = async function(response: any): Promise<boolean> {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId: string = response.id;
const userEmail: string = response.email;
const userAccessToken: string = response.accessToken;
// Try to login on Parse using linkWith and these credentials
// Create a new Parse.User object
const userToLogin: Parse.User = new Parse.User();
// Set username and email to match facebook profile email
userToLogin.set('username', userEmail);
userToLogin.set('email', userEmail);
try {
let loggedInUser: Parse.User = await userToLogin
.linkWith('facebook', {
authData: {id: userId, access_token: userAccessToken},
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get('username')} has successfully signed in!`,
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error: any) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error: any) {
console.log("Error gathering Facebook user info, please try again!")
return false;
}
}
}
return (
<div>
<div className="header">
<img
className="header_logo"
alt="Back4App Logo"
src={
'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
}
/>
<p className="header_text_bold">{'React on Back4App'}</p>
<p className="header_text">{'User Login'}</p>
</div>
{currentUser === null && (
<div className="container">
<h2 className="heading">{'User Login'}</h2>
<Divider />
<div className="form_wrapper">
<Input
value={username}
onChange={(event) => setUsername(event.target.value)}
placeholder="Username"
size="large"
className="form_input"
/>
<Input
value={password}
onChange={(event) => setPassword(event.target.value)}
placeholder="Password"
size="large"
type="password"
className="form_input"
/>
</div>
<div className="form_buttons">
<Button
onClick={() => doUserLogIn()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
block
>
Log In
</Button>
</div>
<Divider />
<div className="login-social">
<FacebookLogin
appId="4573670722644997"
fields="email"
callback={handleFacebookLoginLoginResponse}
render={(renderProps: {onClick: () => void}) => (
<div className="login-social-item login-social-item--facebook">
<img onClick={renderProps.onClick} className="login-social-item__image" src={'https://findicons.com/files/icons/2830/clean_social_icons/250/facebook.png'} alt=""/>
</div>
)}
/>
<div className="login-social-item">
<img className="login-social-item__image" src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN8AAADiCAMAAAD5w+JtAAABWVBMVEX////qQzU0qFNChfT7vAUufPPk7f08gvR0o/Zzofb7uQD7uADpNCP/vQDqPzAspk7pLhrpOysYokLpOyzpNST97OvwgnskpEnsXFH8wgAVoUHpLRj+9/b0paD4xsOIx5fd7+H74uDvenLrU0f61tT1r6vuc2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O8CSg83Qxx8tQ01BXqzVIOSN0uuvB0u2/YHLUZ1vCgyFuAK23U6dV8DydVXl1+696+2+IOz3+677tp5EQNBXV0ewm9Gn98byoe6fM7X+BCwVIbViBOYc6FHV1Ohr3fSSHtXF1IKk+ctbnJcBCATq52BDSsx11VR2MquPZrN+v1Wr9fn82vq/Op2rRUAv7S97+DNSpbRxIh9FHXkj4SUqGpuFpYfwULrYSBGlmoLLT5J7MECSBFN7MQGanCX6RIEZ4odgHnztX8PERDCsUJ2/CdbZA3YyJhMBmJr19XAsC8TkGB0n/j1+OIgy+BdiNKFFuf/bJUZTBt6AaL0HvZga4rfZghLlWIotnoQBb9Pkxj5WguerdCCHi3LJiEKMqwZvNjHvVmwZeFPkxjZegUSgcOer8FsCOCDqbGeTK4CJmKbpuZravmSEKxmuS4W9/so7kSenFbhY1FltGM7N/iVzXt4hXHeStVS+x6JnEq5Phze1RknrGejdOzXYUR+KzgF0g6kRxZ+MmPgveCA6LTebxGISSHtW9zHEcBqEe0WUNkxr7HFWjvdE3YpujUuSXopnOo/o0/DgDlyG7aaZI56vNYzYh1Hla99mHI4/DuoiRor5o6qI/pdxx69MaRT3OTFKKhjqD76QPq0VKSSoVq7S/jWdxQ2UYSuSufUFTG0UdQ6k4qrGyMzFiGOE4NGIXfUEPM7zXI6qHulplbmcxHpAfiJLK37P2WlOrSiQVJV2dM/iKfSBb96uQ0YvTMbMpM4jZHHsomheC7musRfyZjXT0MEp6cTCOx5QSQO1+gOBoBI6vzmKZltsMa+PRFOT2lWVmqBWnVYCbePFi2B9XB7x5G0vy9LSubBndwaA6ZvMPgYgwrM3G1dFgKqnForrC+Jm3rtzVEpKRIAyHNzc1i5sdsmLK/wHcjdWqyATPYAAAAABJRU5ErkJggg=='} alt=""/>
</div>
<div className="login-social-item">
<img className="login-social-item__image" src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAV1BMVEWZmZn///+SkpKVlZWRkZH19fWgoKD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alt=""/>
</div>
</div>
<p className="form__hint">Don't have an account? <a className="form__link" href="#">Sign up</a></p>
</div>
)}
{currentUser !== null && (
<div className="container">
<h2 className="heading">{'User Screen'}</h2>
<Divider />
<h2 className="heading">{`Hello ${currentUser.get('username')}!`}</h2>
<div className="form_buttons">
<Button
onClick={() => doUserLogOut()}
type="primary"
className="form_button"
color={'#208AEC'}
size="large"
block
>
Log Out
</Button>
</div>
</div>
)}
</div>
);
};
Add these classes to your App.css
file if you want to fully render this component’s layout.
App.css
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@import '~antd/dist/antd.css';
.App {
text-align: center;
}
html {
box-sizing: border-box;
outline: none;
overflow: auto;
}
*,
*:before,
*:after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
font-weight: bold;
}
p {
margin: 0;
}
body {
margin: 0;
background-color: #fff;
}
.container {
width: 100%;
max-width: 900px;
margin: auto;
padding: 20px 0;
text-align: left;
}
.header {
align-items: center;
padding: 25px 0;
background-color: #208AEC;
}
.header_logo {
height: 55px;
margin-bottom: 20px;
object-fit: contain;
}
.header_text_bold {
margin-bottom: 3px;
color: rgba(255, 255, 255, 0.9);
font-size: 16px;
font-weight: bold;
}
.header_text {
color: rgba(255, 255, 255, 0.9);
font-size: 15px;
}
.heading {
font-size: 22px;
}
.flex {
display: flex;
}
.flex_between {
display: flex;
justify-content: space-between;
}
.flex_child {
flex: 0 0 45%;
}
.heading_button {
margin-left: 12px;
}
.list_item {
padding-bottom: 15px;
margin-bottom: 15px;
border-bottom: 1px solid rgba(0, 0, 0, 0.06);
text-align: left;
}
.list_item_title {
color: rgba(0, 0, 0, 0.87);
font-size: 17px;
}
.list_item_description {
color: rgba(0, 0, 0, 0.5);
font-size: 15px;
}
.form_input {
margin-bottom: 20px;
}
.login-social {
display: flex;
justify-content: center;
margin-bottom: 30px;
}
.login-social-item {
width: 54px;
height: 54px;
border-radius: 54px;
padding: 12px;
margin: 0 12px;
border: 1px solid #e6e6e6;
box-shadow: 0 2px 4px #d6d6d6;
}
.login-social-item--facebook {
padding: 4px;
background-color: #3C5B9B;
}
.login-social-item__image {
width: 100%;
}
.form__hint {
color: rgba(0, 0, 0, 0.5);
font-size: 16px;
text-align: center;
}
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.
Step 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.
Step 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.
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
const handleFacebookLoginLinkResponse = async function(response) {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId = response.id;
const userAccessToken = response.accessToken;
// Try to link current Parse user using linkWith and these credentials
// Get current user
const userToLink = await Parse.User.current();
try {
let loggedInUser = await userToLink
.linkWith('facebook', {
authData: {id: userId, access_token: userAccessToken},
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get(
'username',
)} has successfully linked his Facebook account!`,
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error) {
console.log("Error gathering Facebook user info, please try again!")
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
const handleFacebookLoginLinkResponse = async function(response: any): Promise<boolean> {
// Check if response has an error
if (response.error !== undefined) {
console.log(`Error: ${response.error}`);
return false;
} else {
try {
// Gather Facebook user info
const userId: string = response.id;
const userAccessToken: string = response.accessToken;
// Try to link current Parse user using linkWith and these credentials
// Get current user
const userToLink: Parse.User = await Parse.User.current();
try {
let loggedInUser: Parse.User = await userToLink
.linkWith('facebook', {
authData: {id: userId, access_token: userAccessToken},
});
// logIn returns the corresponding ParseUser object
alert(
`Success! User ${loggedInUser.get(
'username',
)} has successfully linked his Facebook account!`,
);
// Update state variable holding current user
getCurrentUser();
return true;
} catch (error: any) {
// Error can be caused by wrong parameters or lack of Internet connection
alert(`Error! ${error.message}`);
return false;
}
} catch (error: any) {
console.log("Error gathering Facebook user info, please try again!")
return false;
}
}
}
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.