Permalink
Browse files

put basicAuthenticate into default setup. fixed fetch code in example…

… CRUDCollection.
  • Loading branch information...
1 parent 472b28a commit 8ca831a6d3e368e60d48a2a565a44e454eb5857c @cainus committed Mar 30, 2013
Showing with 53 additions and 35 deletions.
  1. +2 −2 docs/examples.html
  2. +36 −33 lib/Percolator.js
  3. +15 −0 test_server.js
View
@@ -487,8 +487,8 @@
});
},
- fetch : function(req, res, id, cb){
- db.findById(id, function(err, foundObject){
+ fetch : function(req, res, cb){
+ db.findById(req.uri.child(), function(err, foundObject){
if (err){
if (err === 'Not Found'){
return cb(true);
View
@@ -6,6 +6,7 @@ var onBodyHelper = index.onBodyHelper;
var onJsonHelper = index.onJsonHelper;
var fetchHelper = index.FetchHelper;
var AuthenticateHelper = index.AuthenticateHelper;
+var BasicAuthenticateHelper = index.BasicAuthenticateHelper;
var ObjectHelper = index.ObjectHelper;
var StatusManager = index.StatusManager;
var _ = require('underscore');
@@ -87,41 +88,43 @@ Percolator.prototype.defaultRequestHandler = function(req, res, handler, cb){
var that = this;
req.pause();
AuthenticateHelper(req, res, handler, function(){
- fetchHelper(req, res, handler, function(){
- if (!!that.options.parseBody){
- req.resume();
- var ctxt = context(req, res);
- that.mediaTypes.connectMiddleware(ctxt)(req, res, function(err){
- req.body = ctxt.body;
- req.rawBody = ctxt.rawBody;
- if (!!err) {
- if (err.match(/^Parse Error:/)){
- that.statusman.createResponder(req, res).badRequest(err);
- return;
+ BasicAuthenticateHelper(req, res, handler, function(){
+ fetchHelper(req, res, handler, function(){
+ if (!!that.options.parseBody){
+ req.resume();
+ var ctxt = context(req, res);
+ that.mediaTypes.connectMiddleware(ctxt)(req, res, function(err){
+ req.body = ctxt.body;
+ req.rawBody = ctxt.rawBody;
+ if (!!err) {
+ if (err.match(/^Parse Error:/)){
+ that.statusman.createResponder(req, res).badRequest(err);
+ return;
+ }
+ if (err === "Missing Content-Type"){
+ that.statusman.createResponder(req, res).unsupportedMediaType("None provided.");
+ return;
+ }
+ if (err === "Unregistered content-type."){
+ that.statusman.createResponder(req, res).unsupportedMediaType(req.headers['content-type']);
+ return;
+ }
+ console.log("post mediaTypes middleware error:");
+ console.log(err);
+ return cb(err);
+ } else {
+ cb(null, context(req, res));
}
- if (err === "Missing Content-Type"){
- that.statusman.createResponder(req, res).unsupportedMediaType("None provided.");
- return;
- }
- if (err === "Unregistered content-type."){
- that.statusman.createResponder(req, res).unsupportedMediaType(req.headers['content-type']);
- return;
- }
- console.log("post mediaTypes middleware error:");
- console.log(err);
- return cb(err);
- } else {
- cb(null, context(req, res));
- }
- });
- } else {
- req.onBody = onBodyHelper(req, res, handler, function(){
- req.onJson = onJsonHelper(req, res, handler, function(){
- req.resume();
- cb(null, context(req, res));
});
- });
- }
+ } else {
+ req.onBody = onBodyHelper(req, res, handler, function(){
+ req.onJson = onJsonHelper(req, res, handler, function(){
+ req.resume();
+ cb(null, context(req, res));
+ });
+ });
+ }
+ });
});
});
};
View
@@ -48,6 +48,21 @@ server.routeDirectory(resourceDir, app.resourcePath, function(err){
res.end("muahahah!");
}
});
+ server.route('/someProtectedPath', {
+ basicAuthenticate : function(username, password, req, res, cb){
+ // try to get the user here, based on cookie, Authentication header, etc
+ if (username === 'Pierre' && password === 'Collateur'){
+ return cb(null, {username : "Pierre", twitter_handle : "@Percolator"});
+ // user object will be available in req.authenticated in all methods
+ } else {
+ return cb(true); // Percolator will 401 for you
+ }
+ },
+ GET : function(req, res){
+ res.object({youAre : req.authenticated}).send();
+ }
+ });
+
if (err) {
console.log("Routing error");

0 comments on commit 8ca831a

Please sign in to comment.