Book Image

Learn Node.js by Building 6 Projects

By : Eduonix Learning Solutions
Book Image

Learn Node.js by Building 6 Projects

By: Eduonix Learning Solutions

Overview of this book

<p>With its event-driven architecture and efficient web services capabilities, more and more companies are building their entire infrastructure around Node.js. Node has become a de facto part of web development that any serious developer needs to master.</p> <p>This book includes six Node.js projects that gradually increase in complexity. You'll start by building a simple web server and create a basic website. You will then move to create the login system, blog system, chat system, and e-learning system.</p> <p>By creating and following the example projects in this book, you’ll improve your Node.js skills through practical working projects, and you'll learn how to use Node.js with many other useful technologies, such as ExpressJS, Kickstart, and Heroku.</p>
Table of Contents (12 chapters)

Passport login authentication


In the last section, we went ahead and hashed our passwords using bcrypt. We'll now get into our login functionality. We'll use Passport with it, which is an authentication module for Node.js. What's great about Passport is that it's highly customizable; it just gives you a simple layer that sits on top of your application and you can kind of do what you want with it. You can use the LocalStrategy, which is what we'll be doing. This means we'll have a username, password, and a local database. But you can also use things like Facebook login, Twitter login, and a bunch of other types of logins:

Let's go to the Documentation page and then go to Authenticate. This is going to show us that we need a post route to our login and we also need to include this passport.authenticate:

I'll copy the preceding code and go into routes/users.js. We'll go down to login. Paste that in right below it and instead of app, we'll use our router, router.post('/login', authenticate. We...