npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sails-generate-remember

v3.0.2

Published

Generate a Passport.js authentication layer for your Sails app with remember-me option

Downloads

18

Readme

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',
  strategy: require('passport-twitter').Strategy,
  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 -g

Once that's all set, install the generator:

$ npm install sails-generate-remember

Production Usage

On the command line
$ sails generate remember
In a node script
var path = require('path');
var sailsgen = require('sails-generate');
var scope = {
	rootPath: path.resolve(__dirname)
};
sailsgen(require('sails-generate-remember'), 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 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 config/routes.js file:

'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 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', /* your auth dependant policies go here */ ]

Lastly, you will need to install the passport, bcryptjs 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.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: sessionAuth) in api/policies/ and add it to config/policies as follows:

 '*': ['passport', 'rememberMeAuth', '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.

Questions?

See FAQ.md.

More Resources


Copyright © 2016 Adam Sawicki. Licensed under the terms of the MIT license.