Need help with sails-generate-auth?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

406 Stars 68 Forks MIT License 149 Commits 0 Opened issues


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

Services available


Need anything else?

Contributors list



Release Code Climate Dependency Status Downloads

:warning: OBS: This generator is not currently maintained. Check out for an actively maintained project based on this generator.

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

as well as a callback at
- 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.

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-auth

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

, 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
to load your Passport providers on startup.

For the routes, this is what you'll need to add to your

'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

to load your Passport providers on startup by adding the following line:;

All required Passport.js middleware is contained within the

policy so all you need to do is load it before your controllers in
'*': [ 'passport', /* your auth dependant policies go here */ ]

Lastly, you will need to install the

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:

) in
and add it to
as follows:
 '*': ['passport', '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

(available in
). This policy ensures that the API is secure and only requests containing a bearer token can access them.



More Resources

Copyright © 2014-2015 Kasper Kronborg Isager. Licensed under the terms of the MIT license.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.