feathers-authentication
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -47,3 +47,3 @@ 'use strict'; | ||
exports.requireAuth = function (hook, next) { | ||
// Allow user to view records without a userID. | ||
// Allow user to view records without a userId. | ||
if (!hook.params.user) { | ||
@@ -57,13 +57,28 @@ return next(new Errors.NotAuthenticated('Please include a valid auth token in the Authorization header.')); | ||
/** | ||
* Set the userID as the owner. | ||
* Add the current user's id to the query params. | ||
* | ||
* find, get, create, update, remove | ||
* find, get | ||
*/ | ||
exports.setOwner = function (hook, next) { | ||
hook.params.query.userID = hook.params.user._id; | ||
return next(null, hook); | ||
exports.queryWithUserId = function (idInDB, userId) { | ||
// If it's called directly as a hook, use defaults of query.userId and user._id. | ||
if (_typeof(arguments[0]) === 'object') { | ||
console.log('Running setOwner hook with defaults of query.userId and user._id'); | ||
var hook = arguments[0]; | ||
var next = arguments[1]; | ||
hook.params.query.userId = hook.params.user._id; | ||
return next(null, hook); | ||
// otherwise it was run as a function at execution. | ||
} else { | ||
return function (hook, next) { | ||
hook.params.query[idInDB] = hook.params.user[userId]; | ||
return next(null, hook); | ||
}; | ||
} | ||
}; | ||
/** | ||
* Checks that the action is performed by an admin or owner of the userID. | ||
* Checks that the action is performed by an admin or owner of the userId. | ||
* // TODO: Fix this. | ||
* | ||
@@ -74,3 +89,3 @@ * find, get, create, update, remove | ||
if (hook.params.user.admin) { | ||
hook.params.query.userID = hook.params.user._id; | ||
hook.params.query.userId = hook.params.user._id; | ||
} | ||
@@ -81,3 +96,3 @@ return next(null, hook); | ||
/** | ||
* Set the userID as the owner. | ||
* Set the userId as the owner. | ||
* | ||
@@ -88,3 +103,3 @@ * find, get, create, update, remove | ||
if (!hook.params.user.admin) { | ||
hook.params.query.userID = hook.params.user._id; | ||
hook.params.query.userId = hook.params.user._id; | ||
} | ||
@@ -124,3 +139,3 @@ return next(null, hook); | ||
// Allow user to view records without a userID. | ||
// Allow user to view records without a userId. | ||
if (hook.data.email) { | ||
@@ -133,4 +148,4 @@ hook.data.email = hook.data.email.toLowerCase(); | ||
/** | ||
* Authenticated users can have their own records (with userID), | ||
* and non-authenticated users can view records without a userID. | ||
* Authenticated users can have their own records (with their userId), | ||
* and non-authenticated users can view records that have no userId (public). | ||
* | ||
@@ -141,5 +156,5 @@ * find, get, create, update, remove | ||
// If no user, limit to public records (no userID) | ||
// If no user, limit to public records (no userId) | ||
if (!hook.params.user) { | ||
hook.params.query.userID = null; | ||
hook.params.query.userId = null; | ||
return next(); | ||
@@ -152,3 +167,3 @@ } | ||
/** | ||
* Set up the userID on data. | ||
* Set up the userId on data. | ||
* | ||
@@ -159,5 +174,5 @@ * create | ||
// If a user is logged in, set up the userID on the data. | ||
if (hook.params && hook.params.user && !hook.data.userID) { | ||
hook.data.userID = hook.params.user._id; | ||
// If a user is logged in, set up the userId on the data. | ||
if (hook.params && hook.params.user && !hook.data.userId) { | ||
hook.data.userId = hook.params.user._id; | ||
} | ||
@@ -164,0 +179,0 @@ return next(null, hook); |
{ | ||
"name": "feathers-authentication", | ||
"description": "Add Authentication to your FeathersJS app.", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"homepage": "https://github.com/feathersjs/feathers-authentication", | ||
@@ -6,0 +6,0 @@ "main": "lib/", |
@@ -16,3 +16,4 @@ # feathers-authentication | ||
var bodyParser = require('body-parser'); | ||
var feathersAuth = require('feathers-authentication'); | ||
var feathersAuth = require('feathers-authentication').default; | ||
var authHooks = require('feathers-authentication').hooks; | ||
var mongooseService = require('feathers-mongoose'); | ||
@@ -36,5 +37,6 @@ | ||
before:{ | ||
create: [feathersAuth.hashPassword('password')] | ||
create: [authHooks.hashPassword('password')] | ||
} | ||
})) | ||
``` | ||
@@ -68,3 +70,3 @@ | ||
- __userEndpoint__ - The api endpoint used to look up the user service. The default is `'/api/users`. | ||
- __loginEndpoint__ - The url for posting the username and password during login. The default is `/api/login`. | ||
- __loginEndpoint__ - The url for posting the username and password during login. The default is `/api/login`. You can also post a valid token here to receive a new one. You might use this when the current auth token is about to expire to stay logged in on the client. | ||
- __usernameField__ The database field containing the username on the user service. The default is `username`. | ||
@@ -78,2 +80,11 @@ - __passwordField__ The database field containing the password on the user service. The default is `password`. | ||
## Bundled Hooks | ||
The `feathers-authentication` plugin automatically handles auth. Keep in mind that access control is not automatic, but is easy to set up with the included hooks. See the [feathers-hooks](https://github.com/feathersjs/feathers-hooks) plugin and the [FeathersJS website](http://feathersjs.com/learn) for more information about hooks. | ||
#### hashPassword('password') | ||
This is intended to be used on the user service on the `create` method. It will automatically hash the data coming in on the `password` field. You can specify another field by providing another string. | ||
#### requireAuth | ||
## Example | ||
@@ -89,4 +100,4 @@ | ||
var bodyParser = require('body-parser'); | ||
var feathersAuth = require('feathers-authentication'); | ||
var hashPassword = feathersAuth.hashPassword; | ||
var feathersAuth = require('feathers-authentication').default; | ||
var authHooks = require('feathers-authentication').hooks; | ||
@@ -123,3 +134,3 @@ // Initialize the application | ||
userService.before({ | ||
create: hashPassword() | ||
create: authHooks.hashPassword('password') | ||
}); | ||
@@ -170,3 +181,3 @@ | ||
__0.0.2__ | ||
__0.0.5__ | ||
@@ -173,0 +184,0 @@ - Initial release |
26986
422
184