Giter Site home page Giter Site logo

codedeep / sails-generate-auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kasperisager/sails-generate-auth

0.0 1.0 0.0 211 KB

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

Home Page: https://npmjs.org/package/sails-generate-auth

License: MIT License

sails-generate-auth's Introduction

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

sails-generate-auth's People

Contributors

0xgeert avatar ajself avatar dbkaplun avatar handtrix avatar kasperisager avatar staticdreams avatar tsterker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.