sails-generate-remember
v3.0.2
Published
Generate a Passport.js authentication layer for your Sails app with remember-me option
Downloads
18
Maintainers
Readme
sails-generate-auth
A Passport.js-based authentication generator for use with the Sails command-line interface.
So, how easy is it to use? Say you wanted to add Twitter authentication to our app for example – this is all you'd need:
twitter: {
name: 'Twitter',
protocol: 'oauth',
strategy: require('passport-twitter').Strategy,
options: {
consumerKey: 'your-consumer-key',
consumerSecret: 'your-consumer-secret'
}
}
This sets you up with an authentication endpoint at /auth/twitter
as well as a callback at /auth/twitter/callback
- easy, huh?
Behind the scenes, the service uses the concept of "Passports" to store everything related to user authentication. This allows you to keep your own models free of authentication-related bloat as well as help you optimize your application as the data is queried separately only when authentication happens.
I do encourage you to read through the entire source – everything's very well documented, so it should be an easy read.
Installation
Certain generators are installed by default in Sails, but they can be overridden. Check the Sails docs for information on installing generator overrides / custom generators.
In order to use a generator you will need the latest Sails, ~0.10, which can be installed with:
npm install sails -g
Once that's all set, install the generator:
$ npm install sails-generate-remember
Production Usage
On the command line
$ sails generate remember
In a node script
var path = require('path');
var sailsgen = require('sails-generate');
var scope = {
rootPath: path.resolve(__dirname)
};
sailsgen(require('sails-generate-remember'), scope, function (err) {
if (err) throw err;
// It worked.
});
Requirements
The only requirements, besides running the generator and adding some providers in config/passport.js
, is having a set of routes that exposes the authentication endpoints. You'll also need to load the Passport.js middleware for all your controllers and install the required npm packages. Lastly, you need to add a line to config/bootstrap.js
to load your Passport providers on startup.
For the routes, this is what you'll need to add to your config/routes.js
file:
'get /login': 'AuthController.login',
'post /logout': 'AuthController.logout',
'get /register': 'AuthController.register',
'post /auth/local': 'AuthController.callback',
'post /auth/local/:action': 'AuthController.callback',
'get /auth/:provider': 'AuthController.provider',
'get /auth/:provider/callback': 'AuthController.callback',
'get /auth/:provider/:action': 'AuthController.callback',
Next, change your config/bootstrap.js
to load your Passport providers on startup by adding the following line:
sails.services.passport.loadStrategies();
All required Passport.js middleware is contained within the passport
policy so all you need to do is load it before your controllers in config/policies.js
:
'*': [ 'passport', /* your auth dependant policies go here */ ]
Lastly, you will need to install the passport
, bcryptjs
and validator
packages from npm for everything to work correctly.
If you want to make use of the error messages, you'll also need to add the following locale definitions (example translations provided):
{
"Error.Passport.Password.Invalid": "The provided password is invalid!",
"Error.Passport.Password.Wrong": "Whoa, that password wasn't quite right!",
"Error.Passport.Password.NotSet": "Oh no, you haven't set a password yet!",
"Error.Passport.Username.NotFound": "Uhm, what's your name again?",
"Error.Passport.User.Exists": "This username is already taken.",
"Error.Passport.Email.NotFound": "That email doesn't seem right",
"Error.Passport.Email.Missing": "You need to supply an email-address for verification",
"Error.Passport.Email.Exists": "This email already exists. So try logging in.",
"Error.Passport.Username.Missing": "You need to supply a username",
"Error.Passport.Password.Missing": "Oh no, you haven't set a password yet!",
"Error.Passport.Generic": "Snap. Something went wrong with authorization."
}
sails-generate-auth, by default doesn't deny access to controllers if the user is not logged in. For that, you can create another policy (for example: sessionAuth
) in api/policies/
and add it to config/policies
as follows:
'*': ['passport', 'rememberMeAuth', 'sessionAuth'],
'auth': {
'*': ['passport']
}
This helps to restrict access to all the controller except auth controller actions such as login, logout and register, if the user is not logged in. See this issue and stackoverflow answer for more details.
For controller actions which are accessed via APIs, you can add bearerAuth
(available in api/policies
). This policy ensures that the API is secure and only requests containing a bearer token can access them.
Questions?
See FAQ.md
.
More Resources
Copyright © 2016 Adam Sawicki. Licensed under the terms of the MIT license.