Generate a Passport.js authentication layer for your Sails app that will Rock Your Socks™.

npm install sails-generate-auth
6 downloads in the last day
44 downloads in the last week
165 downloads in the last month



NPM version Code Climate Dependency Status

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',
  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.


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.

Production Usage

On the command line
$ sails generate auth
In a node script
var path = require('path');
var sailsgen = require('sails-generate');
var scope = {
    rootPath: path.resolve(__dirname)
sailsgen(require('sails-generate-auth'), scope, function (err) {
    if (err) throw err;

    // It worked.

The only requirements, besides running the generator and adding some providers in config/passport.js, is having a model named "User" in your application as well as 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.

At the very least, your User model needs to look like this:

module.exports = {
  attributes: {
    username  : { type: 'string', unique: true },
    email     : { type: 'email',  unique: true },
    passports : { collection: 'Passport', via: 'user' }

As for the routes, this is what you'll need to add to your config/routes.json file:

'get /login': 'AuthController.login',
'get /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',

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' ]

Lastly, you will need to install the passport, bcrypt 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.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.Email.NotFound": "That email doesn't seem right"


To get started quickly and see this generator in action, run the bin/index.js script:

$ git clone YOUR_FORK_OF_THIS_REPO sails-generate-auth-fork
$ cd sails-generate-auth-fork
$ npm install
$ node ./bin

bin/index.js is a simple script, bundled only for convenience, that runs the generator with hard-coded scope variables. Please feel free to modify that file however you like! Also see for more information on overriding/enhancing generators.



More Resources

Copyright 2014 © Kasper Kronborg Isager. Licensed under the terms of the MIT License

npm loves you