Register your app in Meta's Developer Dashboard
To use the Facebook Provider you will need a client id and a client secret. You will get them while following the process to setup Facebook OAuth 2.0 for your application. Follow the instructions in the following link:
When asked for an origin URL, insert the origin of the url in which your app is hosted.
Examples:
http://localhost:3000https://www.example.com
When asked for a redirect URL (or callback), insert[http or https]://[origin]/[your route for Facebook authentication]
Examples:
http://localhost:8000/auth/facebookhttps://www.example.com/api/login/facebookhttps://www.example.com/godel/russell/myFacebookLogin
Easy-to-read article (Follow the guide until you get a client id and client secret and setup a callback. Don't worry about the rest, Gatekeeper will take care of that): https://theonetechnologies.com/blog/post/how-to-get-facebook-application-id-and-secret-key
Facebook Provider
Options
| Option name | Description | Default value | Required? |
|---|---|---|---|
clientId | Your Facebook OAuth client id | Yes | |
clientSecret | Your Facebook OAuth client secret | Yes | |
callbackURL | The URL that will serve as callback for Facebook OAuth | Yes | |
scope | The OAuth scopes that will be granted to the access token | ['public_profile', 'email'] | No |
To the callbackURL option you must provide the URL you provided when asked for a redirect (or callback) URL when
registering your Facebook OAuth app.
Handler
The Facebook Provider passes three parameters to the handler:
refresh_token: The OAuth refres token. It is usually only given the first time a user signs-in to your applicationaccess_token: The OAuth access token.profile: The information of the Facebook user.
Errors
The predefined errors of this provider are:
UserNotFound
Examples
Example #1
Create a Facebook Provider
import gatekeeper from 'gatekeeper-authentication'
import { FacebookProvider } from 'gatekeeper-authentication/providers/facebook'
const FacebookAuth = new FacebookProvider({
clientId: '<YOUR FACEBOOK CLIENT ID>',
clientSecret: '<YOUR FACEBOOK CLIENT SECRET>',
callbackURL: 'https://yourdomainorlocalhost.com/auth/facebook'
}, function handler(refresh_token, access_token, profile) => {
const user = User.findOne({ facebookId: profile.id })
if (user == null) {
// For example, if the user does not exist, create it
User.create({
username: profile.name,
email: profile.email
});
}
return user;
});
Login with a Facebook Provider
Important: When using the Facebook provider on login routes, make sure you use gatekeeper.authenticateWithProvider(yourFacebookProvider) in a GET route, as the user will be redirected to Facebook's sign in page
router.get(
'/auth/facebook',
gatekeeper.authenticateWithProvider(FacebookAuth), (req, res) => {
return res.redirect('/profile');
}
);