connect-roles
Advanced tools
Comparing version 0.0.1 to 0.1.0
29
index.js
@@ -5,4 +5,11 @@ var functionList = []; | ||
}; | ||
var defaultUser = {}; | ||
module.exports = function middleware(req, res, next){ | ||
var oldUser = req.user; | ||
req.user = req.user || Object.create(defaultUser); | ||
if(oldUser){ | ||
req.user.isAuthenticated = true; | ||
}else{ | ||
req.user.isAuthenticated = false; | ||
} | ||
if(req.user){ | ||
@@ -12,9 +19,2 @@ req.user.is = tester(req); | ||
} | ||
req.userIs = tester(req); | ||
req.userCan = tester(req); | ||
if(req.user) | ||
req.isAuthenticated = true; | ||
else | ||
req.isAuthenticated = false; | ||
next(); | ||
@@ -29,8 +29,10 @@ }; | ||
module.exports.isAuthenticated = function(req,res,next){ | ||
if(arguments.length != 3) return module.exports.isAuthenticated; | ||
if (req.user) next(); | ||
else failureHandler(req, res, next, action); | ||
if(arguments.length === 0) return module.exports.isAuthenticated; | ||
if (req.user && req.user.isAuthenticated) next(); | ||
else if(req.user) failureHandler(req, res, "isAuthenticated"); | ||
else throw "Request.user was null or undefined, include middleware"; | ||
}; | ||
module.exports.authStrategy = function(path, fn){ | ||
module.exports.useAuthorisationStrategy = | ||
module.exports.useAuthorizationStrategy = function(path, fn){ | ||
if(typeof path === "function"){ | ||
@@ -51,2 +53,5 @@ fn = path | ||
}; | ||
module.exports.setDefaultUser = function(user){ | ||
defaultUser = user; | ||
}; | ||
@@ -53,0 +58,0 @@ |
{ | ||
"name": "connect-roles", | ||
"description": "Provides dynamic roles based authentication for node.js connect and express servers.", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"homepage": "http://documentup.com/Tuskan360/connect-roles", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -28,3 +28,3 @@ | ||
To define authentication strategies, call the authStrategy function: | ||
To define authentication strategies, call the useAuthorisationStrategy function: | ||
@@ -41,3 +41,3 @@ @param [path] {string} The action/path/ability/role that this strategy applies to. The strategy will be ignored for all other roles/abilities. If it is not present, the strategy is used for all roles/abilities. | ||
```javascript | ||
user.authStrategy(function(user, action, stop){ | ||
user.useAuthorisationStrategy(function(user, action, stop){ | ||
//User logic here. | ||
@@ -48,3 +48,3 @@ }); | ||
user.authStrategy("create user", function(user, action, stop){ | ||
user.useAuthorisationStrategy("create user", function(user, action, stop){ | ||
//User logic here. | ||
@@ -61,8 +61,8 @@ }); | ||
```javascript | ||
user.authStrategy("register", function(user){ | ||
if(!user) return true; | ||
user.useAuthorisationStrategy("register", function(user){ | ||
if(!user.isAuthenticated) return true; | ||
}); | ||
user.authStrategy(function(user, action, stop){ | ||
if(!user){ | ||
user.useAuthorisationStrategy(function(user, action, stop){ | ||
if(!user.isAuthenticated){ | ||
stop(action === "anonymous"); | ||
@@ -78,4 +78,4 @@ } | ||
```javascript | ||
user.authStrategy(function(user, action){ | ||
if(user){//You can remove this if already checking for anonymous users | ||
user.useAuthorisationStrategy(function(user, action){ | ||
if(user.isAuthenticated){//You can remove this if already checking for anonymous users | ||
for(var i = 0; i<user.roles.length; i++){ | ||
@@ -93,4 +93,4 @@ if(user.roles[i] === action) return true; | ||
```javascript | ||
user.authStrategy("edit user", function(user, action){ | ||
if(user){//You can remove this if already checking for anonymous users | ||
user.useAuthorisationStrategy("edit user", function(user, action){ | ||
if(user.isAuthenticated){//You can remove this if already checking for anonymous users | ||
if(req.params.userid){ | ||
@@ -155,4 +155,4 @@ if(user.id === req.params.userid){ | ||
```javascript | ||
user.authStrategy("edit user", function(user, action){ | ||
if(user){//You can remove this if already checking for anonymous users | ||
user.useAuthorisationStrategy("edit user", function(user, action){ | ||
if(user.isAuthenticated){//You can remove this if already checking for anonymous users | ||
if(req.params.userid){ | ||
@@ -175,8 +175,8 @@ if(user.id === req.params.userid){ | ||
```javascript | ||
user.authStrategy("register", function(user){ | ||
if(!user) return true; | ||
user.useAuthorisationStrategy("register", function(user){ | ||
if(!user.isAuthenticated) return true; | ||
}); | ||
user.authStrategy(function(user, action, stop){ | ||
if(!user){ | ||
user.useAuthorisationStrategy(function(user, action, stop){ | ||
if(!user.isAuthenticated){ | ||
stop(action === "anonymous"); | ||
@@ -217,2 +217,12 @@ } | ||
}); | ||
``` | ||
## Default User | ||
By default, the user middleware will set the user up to be `{}` and will then add the property `isAuthenticated = false`. | ||
Roles will always add `isAuthenticated = false` but you can configure a default user object as follows. | ||
```javascript | ||
user.setDefaultUser({id:"anonymous"}); | ||
``` |
12853
169
219