This tutorial is out of date and no longer maintained.
Note: This article is part of our Easy Node Authentication series.
Welcome to Part 2 of our Easy Node Authentication series. We will be using the foundation of that tutorial to use Facebook authentication with our application. We already have a good application structure for our application packages, user model, application setup, and views.
Note: Edit 11/18/2017: Updated to reflect Facebook API changes.
config/auth.js
)config/passport.js
)app/routes.js
)views/
)This will be a much shorter tutorial than the last one since we don’t have to set up our entire Node application from scratch. This is our code structure from the first tutorial. If you need those files, go read that tutorial.
- app
------ models
---------- user.js <!-- our user model -->
------ routes.js <!-- all the routes for our application -->
- config
------ auth.js <!-- will hold all our client secret keys (facebook, twitter, google) -->
------ database.js <!-- will hold our database connection settings -->
------ passport.js <!-- configuring the strategies for passport -->
- views
------ index.ejs <!-- show our home page with login links -->
------ login.ejs <!-- show our login form (only needed for local login) -->
------ signup.ejs <!-- show our signup form (only needed for local signup) -->
------ profile.ejs <!-- after a user logs in, they will see their profile -->
- package.json <!-- handle our npm packages -->
- server.js <!-- setup our application -->
Let’s get down to business.
First thing’s first, let’s go to the Facebook Developers Portal and create our application. Once we do that, we will have the credentials we need for logging in with Facebook.
Set your callback URL to http://localhost:8080/auth/facebook/callback
. This will ensure that our application will authenticate with Facebook and redirect users back to our application after they have approved access for our application. The callback will be where we store the user details that we need.
Live vs Sandbox: Having your application in sandbox mode means only you can log into the application. Live means logins are available for everyone.
Now that we have our App ID and App Secret, let’s set up our config file. We’ll add the necessary fields for our Twitter and Google auth also so we can just update that in our future Node Authentication tutorials.
Fill in your clientID (app id) and clientSecret (app secret) and we’ll be using this data when we configure our Passport Facebook Strategy.
config/passport.js
Now that we have our Node application set to access our Facebook application, let’s configure our Facebook strategy. This strategy will be used to authenticate with Facebook and handle the callback.
The code from our Local Strategies will be in this file, we’ll just add our Facebook Strategy below them.
Profile: The callback will pass back user profile information and each service (Facebook, Twitter, and Google) will pass it back a different way. Passport standardizes the information that comes back in its profile object. For more information on how Passport formats its user profile, visit the user profile doc.
We will use this strategy to authenticate with Facebook and handle the callback. We will also store some user information and the Facebook OAuth token.
Now that it is all set up, we just have to link to it in our routes.
app/routes.js
Remember when we set up our application, our passport object is passed from the server.js
file to the config/passport.js
file. Then it is passed to the routes. This is why we can just add our Facebook Strategy straight to the config file and have it work in routes.
This is the simplest part. We will need two routes: one for authentication and one for handling the callback.
By default, Facebook will provide you with user information, but not the email address. We will add this by specifying the scope. You can also add in more scopes to access more information.
When logging in with Facebook, try to use the fewest permissions that you need. You want your users to feel comfortable about their privacy when logging into and using your application.
index.ejs
and profile.ejs
Now that we have our routes, we just need to provide users with a button to authenticate with Facebook. Then after they are authenticated and redirected to the authentication required profile page, we want to display their information.
We’ll add this button to our index.ejs
file.
Now we have the login button for Facebook. It will point to the route we just created and then the callback will redirect the user to the profile page upon successful authentication.
For simplicity’s sake, I’ve just removed the links from the last tutorial for local login and signup. You can keep them, but they aren’t important. The last tutorial of this series will combine all the forms of login so we’ll deal with those then.
When a user clicks that button, they will be taken to our /auth/facebook
route where they will be passed to the Passport Strategy. There they will be sent to Facebook for authentication.
If a user presses cancel, they will just be redirected back to our home page. This is set in the failureRedirect
of our /auth/facebook/callback
route. If successful, they will be saved to the database or logged in and redirected to /profile.
views/profile.ejs
The last thing we need to do is to show off our user information on our profile page.
We can see the user below in our database:
We have our user with the Facebook information that we set. Now we show that user on their profile page.
Now we have our user logged in at their profile page.
That wasn’t too much work! Since our application was already set up in the last tutorial, we only had to add a few components. We have configured Passport for Facebook, added our routes, authenticated with Facebook, and shown our user’s profile.
In the next article, we will be tackling Twitter authentication using Passport. This will be very similar to the Facebook authentication so if you want to try to figure out Twitter authentication, I’d definitely encourage it. Until next time!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!