Sails JS ,Authentication using passportJS

In this article ,we will learn how to login / signUp using passportJS in Sails app.You can fork the repo or follow me at github for furhter update of the code :

This is how your app will look like :

Homepage without logged in ,you can see the sigup and login button at nav bar:

signUp page

login page

after logged in homepage you can see the email at navigation

  1. First install passportJS and all it’s dependencies .You can refer stackoverflow or officail sites/npm to install,after completing create new project : sails new project_name

2. we create a auth and user api using the command :

sails generate api user

sails generate api auth

3. add this code to your authController.js

4.and in User.js in model

5.Now your model is created .Add the passport.js file in config folder

6.Add in views your pages : signup.ejs ,login.ejs , layout.ejs

7.So you have competed the Model,View and controller part .Main thing is routing .Edit yout route.js file as follows:

8.Now open your terminal and run : sails lift

after Signup you will be redirected to this json page:

Now go to http://localhost:1337 and go to login button and enter your email and password if it is corret then you will be redirected to this:

and when you come back to http://localhost:1337/ then you will find you email at the navigation bar.

Yeah that’s it you have done this basic app.If you still find any type of error then you can refer the code from this github link:

I will update my github repo later ,so follow me and fork this repo for further update.I am going to convert this to a blog where authenticated user only can create a new blog and normal user only can see the post.

So keep touch with me and wait for the next episode.

#happyCoding :D