Generate a Passport.js authentication layer for your Sails app that will Rock Your Socks™.
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'Strategyoptions: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.
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
$ sails generate auth
var path = require'path';var sailsgen = require'sails-generate';var scope =rootPath: pathresolve__dirname;sailsgenrequire'sails-generate-auth' scopeif err throw err;// It worked.;
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
'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''get /auth/:provider/:action': 'AuthController.callback'
Next, change your
config/bootstrap.js to load your Passport providers on startup by adding the following line:
All required Passport.js middleware is contained within the
passport policy so all you need to do is load it before your controllers in
'*': 'passport' /* your auth dependant policies go here */
If you want to make use of the error messages, you'll also need to add the following locale definitions (example translations provided):
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:
api/policies/ and add it to
config/policies 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
bearerAuth (available in
api/policies). This policy ensures that the API is secure and only requests containing a bearer token can access them.