Home » Node.js

How to setup and use passport OAuth Facebook Authentication (Section 1) | Node.js

Node.js Authentication Series: Here, we are going to learn How to setup and use passport OAuth Facebook Authentication (Section 1) in Node.js?
Submitted by Godwill Tetah, on October 04, 2019

In my last articles, we looked at the implementation of the passport-local authentication strategy. We also looked at the various requirements to get started with the login form.

Here are the previous articles,

In this article, we will look at another form of authentication called the OAuth authentication which involves sign in or signup using social media.

Don't worry that much about the big term OAuth... you'll get familiar with them as you work with Node.js more often.

My goal here is to make it simpler for understanding and implementation.

These codes are just to help you get started. So you can edit them at any time to confirm your desires.

Note: You should have a basic understanding of Node.js, Express, MongoDB database and HTML.

In this first section, we will set up our Express app with some routes and our HTML form.

In the second section, we'll finally set up the authentication strategy it's self on Facebook developers platform and tests our code.

Cheers!!!

Create a new folder for our project where all files will be stored.

Setup your Express Server.

Require all necessary modules and dependencies.

Create a file app.js and type the following code,

/*  EXPRESS SETUP  */

const express = require('express');
const app = express();

app.get('/', (req, res) => res.sendFile('index.html', {
    root: __dirname
}));

const port = process.env.PORT || 8080;
app.listen(port, () => console.log('App listening on port ' + port));

The code above creates an express server with port 3000 and a route that will read our index.html file.

Next, let's create our simple HTML file... (you can at styles as you wish later).

Create a file called index.html in your project folder and type the following HTML code.

<html>

<head>
    <title>Node.js OAuth</title>
</head>

<body>
    <center>
        <a href=auth/facebook>Sign in with Facebook</a>
    </center>
</body>

</html>

Now, let's install passport-facebook and start looking at what we need for our OAuth facebook authentication.

To install passport module for facebook authentication, run the following command on the terminal.

passport OAuth Facebook Authentication

Let's then configure our strategy and set up routes for success and failure authentication.

Open the app.js file and add the following code below,

/*  CONFIGURATION  */

const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

//success route
app.get('/success', (req, res) => res.send("You have successfully logged in"));

//error route
app.get('/error', (req, res) => res.send("error logging in"));

passport.serializeUser(function(user, cb) {
    cb(null, user);
});

passport.deserializeUser(function(obj, cb) {
    cb(null, obj);
});

The code above configures the module and sets up the error and success route.

The success route function runs when authentication is successful while the error route runs when there's an error.

So if the user successfully logs in with his or her Facebook account, the web page will display ''you have successfully logged in''

And that's it guys for the first section of this tutorial...

You can also visit the official website of passport to learn more @ http://www.passportjs.org/

Read next: How to setup and use passport OAuth Facebook Authentication (Section 1) | Node.js

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.