GitXplorerGitXplorer
O

sails-hook-kimyjwt

public
3 stars
0 forks
2 issues

Commits

List of commits on branch master.
Unverified
250fa9d11800ee4370243e54e5425542867ca0b3

Merge pull request #9 from OrlSan/version2

OOrlSan committed 8 years ago
Unverified
9f5f54c366885981c22d8c4a305537e59cd9f5ee

Version 0.2.0

OOrlSan committed 8 years ago
Unverified
14090cc6b5048717bcbeb389ddbe456ba74d53c8

Add config for Sails responses and passing the payload in the req object

OOrlSan committed 8 years ago
Unverified
80d67ae59ecf40b2ffa290815a29184dd836b61d

Merge pull request #7 from OrlSan/magic-object

OOrlSan committed 8 years ago
Unverified
97bd92db58cd0d3adafabd15f380fde39ddddb9e

Documentation on the magicObject settings

OOrlSan committed 8 years ago
Unverified
f7f4ddf39c2e4aad16d46b25cacd8fac80857abc

Include magicObject support in the settings

OOrlSan committed 8 years ago

README

The README file for this repository.

Kimy JWT

NPM Version NPM Downloads

Sails.js hook for JWT-based authentication, for humans.

If you're looking for using JSON Web Tokens on your Sails apps, you're on the right place.

Use

Install with

$ npm install sails-hook-kimyjwt

And then create the config on config/kimyjwt.js

module.exports.kimyjwt = {
  // Required
  model: "user",
  secretField: "secret",
  // Optional
  idField: "id", // This is an attribute in the model
  passportLike: false // defaults to true
}

Next, you should only add the kimyjwt policy to the routes you require the authentication and you're done:

'get /user/protected/route': [{
    policy: 'kimyjwt'
  }, {
    controller: 'UserController',
    action: 'mySecureRoute'
  }]

Options

Passport.js-like API

If you enable the Passport.js-like API then you can use the req.user object as you usually do in a Passport.js-based application:

// UserController

module.exports = {
	secureRoute: function(req, res) {
		res.json({
			success: true,
			message: "Welcome, " + req.user.name // Name is a property in the model
		});
	}
};

Magic Object in req.user

With the passportLike option enabled, the hook can attach the full model object to the req.user object. This adds the possibility to the following code to be used:

// someController.js

module.exports = {
  myRoute: function(req, res) {
    req.user.someMethodInTheModel();
    // ...

    res.json({
      "success": true,
      "message": "Well done, " + req.user.name + "!"
    });
  }
};

For doing so just add the magicObject setting to true in the options

module.exports.kimyjwt = {
  // Required
  model: "user",
  secretField: "secret",
  // Optional
  idField: "id", // This is an attribute in the model
  passportLike: true, // defaults to true
  magicObject: true // defaults to false
}

Payload to the req object

If you'd like to get the data contained in the JWT Payload in your controllers, then you should only enable the option passPayloadInReq (which defaults to false) in the config file for the module.

module.exports.kimyjwt = {
  // Required
  model: "user",
  secretField: "secret",
  // Optional
  idField: "id",
  passportLike: true,
  passPayloadInReq: true
}

So the following code can be used:

// someController.js

module.exports = {
  myRoute: function(req, res) {
    var issuedAt = req.payload.iat;

    console.log("This token was issued at: " + new Date(issuedAt));

    res.json({
      message: "I think we're all good right now"
    });
  }
};

Use the Sails.js defined responses instead

If you create a unauthorized response in the api/responses folder you can use it instead of the traditional more Express.js-styled response included by default in the module. So, if your response is defined this way:

// unauthorized.js
module.exports = function unauthorized (data, options) {
  var req = this.req;

  return res.status(401).json({
    authorized: false,
    message: "You shall not pass"
  });
};

You can enable this response for being the default one to be sent in the configs

module.exports.kimyjwt = {
  // Required
  model: "user",
  secretField: "secret",
  // Optional
  idField: "id",
  passportLike: true,
  useSailsResponses: true
}

Contribute

All PR and Issues are welcome. You can get in touch with @SoyOrlSan too.

About

(C) 2016, Orlando Sánchez & Jorge Santiago Álvarez, Grupo Jaque.