Implement Passport.js authentication with Sails.js 0.12

Hi guys. In this blog I am going to tell how  to implement Passport.js with  LocalStrategy  in our web application  So, you start your new web app project and you decide to build it using the popular MVC framework Sails.js. We have also decided to use the other widely used security library Passport.js to handle authentication.

Step 1 : Dependencies

Before using Passport.js we need to install some required dpendencies in our application like :

1. npm install passport   –save

2. npm install passport-local –save

3. npm install bcrypt  –save

4. npm install sails-mysql  –save

see all Dependencies in your project directory/package.json

Step 2: Create user model

To create user model run the following command:


The User Model will be created inside the
/api/models/User.js                                                                      and To add the necessary attributes and remove confidential attribute from view edit the newly created file User.js like this :

Step 3: Create  Controller AuthController
Now we have our User model configured, we need to add a controller to  handle authorisation.

Created the controller path =  api/controllers/AuthController.js  Open this file and configure it as like :


Step 4: Define the passport Strategy (Local Strategy i am doing)

Create the service in  api/services/passport.js  copy this code and paste in passport.js file

Step 5:   Configure the  Middleware config/passport.js 

we need to configure the Middleware in Sails to accept it Create a file with name of passport.js in config Directory Structure of your Project.

Step 6:  Configuring Routes

To make sure our users can login and process, we need to configure the Sails routing . open the file config/routes.js add in the following lines :

Step 7:  Create login Page 
Create a login page in  your project directory as flow views/login-page.ejs like this

Step 8: Test your web application


That’s all

Happy Coding !