Socket
Socket
Sign inDemoInstall

sails-generate-auth

Package Overview
Dependencies
3
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sails-generate-auth

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


Version published
Maintainers
1
Created

Readme

Source

image_squidhome@2x.png

sails-generate-auth

Release Code Climate Dependency Status Downloads

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.

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@beta -g

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.
});
Requirements

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. Lastly, you need to add a line to config/bootstrap.js to load your Passport providers on startup.

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

module.exports = {
  
  // Enforce model schema in the case of schemaless databases
  schema: true, 
  
  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',

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

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"
}

Development

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 CONTRIBUTING.md for more information on overriding/enhancing generators.

Questions?

See FAQ.md.

More Resources


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

Keywords

FAQs

Last updated on 22 Jun 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc