Comparing version 0.0.9 to 0.0.10
@@ -56,2 +56,4 @@ //Authentication setup | ||
} | ||
var isAuthenticated=false; | ||
var errorMessage=""; | ||
if (checkUrl(req.url,req.method.toLowerCase(),settings.secureRoutes)){ | ||
@@ -61,24 +63,16 @@ if (token){ | ||
if (!tokenValid){ | ||
res.statusCode=401; | ||
res.statusText="Token expired"; | ||
res.send(); | ||
errorMessage="Token expored"; | ||
}else //Authorized request | ||
{ | ||
if (settings.afterAuthorized){ | ||
var canProceed=settings.afterAuthorized(token); | ||
if (settings.onTokenValid){ | ||
var canProceed=settings.onTokenValid(token); | ||
if (!canProceed){ | ||
res.statusCode=401; | ||
res.statusText="User disabled"; | ||
res.send(); | ||
errorMessage="User disabled"; | ||
}else | ||
{ | ||
req.authInfo=token; | ||
req.isAuthenticated=true; | ||
next(); | ||
isAuthenticated=true; | ||
} | ||
}else | ||
{ | ||
req.authInfo=token; | ||
req.isAuthenticated=true; | ||
next(); | ||
isAuthenticated=true; | ||
} | ||
@@ -88,11 +82,24 @@ } | ||
{ | ||
res.statusCode=401; | ||
res.statusText="Invalid token"; | ||
res.send(); | ||
errorMessage="Invalid token"; | ||
} | ||
}else | ||
{ | ||
req.authInfo=token; | ||
isAuthenticated=true; | ||
} | ||
if (isAuthenticated){ | ||
req.authToken=token; | ||
req.isAuthenticated=true; | ||
if (settings.onAuthenticated){ | ||
settings.onAuthenticated(req,token); | ||
} | ||
next(); | ||
}else | ||
{ | ||
res.statusCode=401; | ||
res.statusText=errorMessage; | ||
if (settings.onUnauthorized){ | ||
settings.onUnauthorized(req,token); | ||
} | ||
res.send(); | ||
} | ||
@@ -99,0 +106,0 @@ }); |
{ | ||
"name": "bearer", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Bearer authentication module using token and Authorization HTTP header", | ||
@@ -5,0 +5,0 @@ "main": "bearer.js", |
@@ -17,2 +17,4 @@ BearerJS | ||
var app = express(); | ||
//Setup authentication | ||
//This should be done before all routes are configured to assure that authorization will be first to execute | ||
bearer({ | ||
@@ -26,2 +28,3 @@ //Make sure to pass in the app (express) object so we can set routes | ||
//If your user is not valid just return "underfined" from this method. | ||
//Your token will be added to req object and you can use it from any method later | ||
var username=req.body.username; | ||
@@ -48,3 +51,3 @@ var userValid=true; //You are aware that this is where you check username/password in your DB, right!? | ||
}, | ||
afterAuthorized:function(token){ | ||
onTokenValid:function(token){ | ||
//This is in case you would like to check user account status in DB each time he attempts to do something. | ||
@@ -55,2 +58,8 @@ //Doing this will affect your performance but its your choice if you really need it | ||
}, | ||
onAuthenticated: function(req, token){ | ||
console.log("this will be executed if request is OK"); | ||
}, | ||
onUnauthorized: function(req, token){ | ||
console.log("this will be executed if request fails authentication"); | ||
}, | ||
secureRoutes:[ | ||
@@ -68,5 +77,16 @@ {url:'/users', method:'get'} | ||
* validateToken: This method will provide you with decrypted token from request. Use it wizely to verify that it is ok | ||
* afterAuthorized: Sometimes you will not want to rely only on token validation. Once request is validated using token, you do additional check (perhaps check status in db etc.) | ||
* onTokenValid: Sometimes you will not want to rely only on token validation. Once request is validated using token, you do additional check (perhaps check status in db etc.) | ||
* onAuthenticated: In case you want to do something when request is authenticated (ex. log something) | ||
* onUnauthorized: In case that you want to do something when request is not authenticated | ||
* secureRoutes: Just add routes you want to have secured | ||
Your TOKEN will be added to request and you can access it in any other action later. For example: | ||
```javascript | ||
router.get('/someroute', function(req, res) { | ||
console.log(req.authToken); | ||
res.send('Respond with a resource'); | ||
}); | ||
``` | ||
On your Client app | ||
@@ -73,0 +93,0 @@ ------------------ |
10009
98
110