Category: Node.js

Node.js Authentication

A lot of sites add a members section, where users are authenticated by means of a username and password. Once the user is logged in successfully, he or she can then gain access to the restricted areas of the site. You are probably here because you are seeking for a simple user authentication using Node.js, well you came to the right place.

What I am offering on this tutorial is a complete script for Login, Registration and Account page. The project is available on github – Download on Github. I kept everything well documented on my codes, but feel free to contact me or leave a comment bellow if you have any questions or if you found a bug.

Modules used:

For the base of our application, we are going to use the framework: Express. Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node based Web applications. For the user authentication, we are going to use Passport.js local strategy. Passport is an authentication middleware for Node.js. it is extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. For our database we are going to use MongoDB. MongoDB is an open-source document database, and leading NoSQL database. MongoDB obviates the need for an Object Relational Mapping (ORM) to facilitate development. And lastly, for the password hashing algorithm we are going to utilize Crypto MD5.

Other modules that I used on this project:

  • async
  • connect-flash
  • jade
  • monk


Registration Page


Login Page:


Account Page:


How do I get setup?

  1. Create a folder called “data” in the root directory of our nodejs project. This is where MongoDB documents will be stored.
  2. Go to MongoDB installation directory and under the bin folder run this command: mongod –dbpath C:\Users\Carl\Documents\nodejs-auth\data This will start the MongoDB server. Leave this CLI instance open and start another CLI instance.
  3. In the new CLI, navigate to where you pulled this repository, ex. C:\Users\Carl\Documents\nodejs-auth, then type-in: npm install then wait till it finishes installing all the modules required to run our Node.js Web Application.
  4. Once the installation is completed, type in the following command to run our Web Application: npm start Make sure to keep the CLI opened.
  5. Now go to using your favorite browser.

Node.js AJAX Pagination with MongoDB + Search + Sort + Filter

For this tutorial we will be using Jade Templating, you can use other templating modules but I’m afraid I wont be able to provide you further support. Jade templating is easy to learn, if you know HTML then it should only take you a few minutes to learn Jade. One rule you have to take note when using Jade is to be very careful with the spaces, this tutorial make use of tabs for indention. Another thing to note when using Jade is you can only choose one kind of indention – spaces or tabs, but you can’t use both at the same time.

This tutorial make use of Express Module so you should have that setup before getting started. I would assume that you already have basic knowledge working with MongoDB and that you already have it set up in your computer.

Required Modules

Let’s Start Coding

We first create all the fields for our search, sort and filters as well as the container that we will be using for rendering the pagination. You can save the code bellow in a file named products.jade under your views folder

extends layout

block content
            input(type='hidden', value='')
                    label Per Page: 
                        option(value='20') 20
                        option(value='50') 50
                        option(value='100') 100
                label Search Keyword: 
                    input.form-control.post_search_text.m-b(type='text', placeholder='Enter a keyword')
                    label Order By: 
                        option(value='name') Title
                        option(value='price') Price
                        option(value='quantity') Quantity
                        option(value='ASC') ASC
                        option(value='DESC') DESC
                input.btn.btn-primary.post_search_submit.m-b(type='submit', value='Filter')

Include MongoDB Connection in your app.js

/* MongoDB connection */
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1'); // 27017 is the default port for our MongoDB instance.

To make our DB accessible to our router, add the following lines in your app.js (just above app.use(‘/’, routes);)

app.use(function(req, res, next){
    req.db = db;

Client Side

The script bellow is the handler of all user clicks coming from the front-end. You can save it in a file called MyPaginationClass.js under public/javascripts. Make sure to include this script file in your layout.jade 

Click here to download the FULL TUTORIAL