DEV Community

Sumit Chahar
Sumit Chahar

Posted on

Creating an OAuth application with Express & Passport in 5 simple steps

In this series of articles we will be creating a basic Github OAuth application using Express.js. We'll also use Passport.js as our authentication middleware.

We will be using mongoDB, mongoose for the database and also use session storage for this application.

To setup our app we need to follow the following steps -

  • install node, npm, mongoDB, express, express generator if not installed.
  • create a project folder and in your project folder use the express generator to initialise our application.
  • while creating the express application make sure we have installed the following dependencies these will be available in package.json -
  1. express
  2. passport
  3. passport-github
  4. express-session
  5. dotenv
  6. connect-mongo
  7. debug
  8. ejs (or any templating engine)
  9. mongoose
  10. http-errors
  11. morgan

We will setup our ejs templates first -

In the views folder we will create our templates since this will be only a backend application.

we will just add a link that points to our OAuth route for now, you can create these to your liking later. You can also create success, failure views, etc.

Image description

The folder structure after we complete our app will look like this for reference -

Image description

Creating the application

There are 5 steps in which we will be creating our Github OAuth application -

  1. Register Application.
  2. Handle Routes.
  3. Create Strategy for Github OAuth.
  4. Adding copy of user data in our local database.
  5. Serialize & deserialise.

Go to next article 👉

Top comments (0)