This is a new tutorial on Sails . Many of you were asking how to implement authentication in Sails. Here i’ve an awesome tool called Waterlock which enables easier authentication methods.
What is Waterlock ?
As its official website says , Waterlock is an all encompassing user authentication/json web token management tool, built for Sails. It provides user authentication , json webtokens and customisation.Waterlock provides you nice generators, so you can get up and running fast spend less time building structure and more time working on features!
So let’s get started.
Create a sails app
Move to the Sails app directory.
As every other node module , waterlock is also installed using npm. You have an advantage here, that this tool is exclusively built for Sails.
Authentication itself is handled via modular libraries, making Waterlock more lightweight. The current auth libraries are listed below.
Using Local Auth
Install local auth.
Generate local auth
The executable script provided with Waterlock
./node_modules/.bin/waterlock gives you all the power you need to generate the different components of Waterlock.
Execute the command -
You will see the list of resources Waterlock provides you.These are generated dynamically based on the auth method you have used. Let us choose
all for now.
Now you can see the list of files generated by waterlock.
Let us see the config file now.You have following options
baseUrl- this is the URL your app resides at, used in password reset urls
jsonWebTokens- object containing information on how the jwt's should be constructed
Configure with your app
Let us try authentication now. Create a new api
In Post Controller file add new function
restricted as given below.
Now let us apply some policies here . Sails uses basic sessionAuth. So we will add this to policies file in sails.
Let us lift the app again.Let us see what happens.
Let us go to
http://localhost:1337/post/open You can see the following
Yeah !!! This is open to all users . So let us see what happens when we load our restricted page.
Yeah!!! Authentication works. You see the restricted access page. So now let us create a user and check whether the authentication works correct.
Create a user
You can create a user by POSTing data to
I’ve used POSTMAN for creating a post request to the AuthController
You’ll be automatically logged in when you create a user. So let us check the
restricted page once again.
Yeah!!! It works perfect. This was a tutorial developed on Session based authentication adapted from the Intro given by Waterlock. Let us see how to create a JSON token based authentication and create a complete user login/registration system with Waterlock,Angular and obiviously Sails in next tutorials.
So this is the end of this tutorial. Let us see in the next tutorial, bye :)