basic-auth
basic-auth copied to clipboard
Protect / but specific exceptions
Hi there, I want to protect "/" but certain paths shall be accessible without a passwort. How can I realize that?
You would need to change this package, here is a quick hack you could try...
basic-auth.js:21> handle: basicAuth(this.callback, this.realm)
-
This line is calling the Connect
basicAuth
module. -
The return value from executing
basicAuth(this.callback, this.realm)
is a function that takes(req, res, next)
-
Create a handler function that takes
(req, res, next)
and wraps the call tobasicAuth(this.callback, this.realm)
. It will be something like this, although my JS is a little rusty...basic-auth.js:21> handle: function(req, res, next) { // inspect request logic goes here, then do this... if(excluded) { next(); } else { (basicAuth(this.callback, this.realm))(req, res, next); } }
-
Inspect the
req
object in your function to determine whether it should execute the basic auth logic -
Please send me a pull request if you develop something flexible.
I have tried your suggestion with the code, without any excluding logic, which should work same as unmodified package before
for(var i=0; i<routes.length; i++) {
WebApp.connectHandlers.stack.splice(i, 0, {
route: routes[i],
handle: function (req, res, next) {
console.log(req.url);
(basicAuth(this.callback, this.realm))(req, res, next)
}
});
}
but I have this error then:
W20180530-22:00:41.215(2)? (STDERR) TypeError: Cannot read property 'length' of undefined
W20180530-22:00:41.216(2)? (STDERR) at /Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/basic-auth-connect/index.js:82:18
W20180530-22:00:41.216(2)? (STDERR) at Object.handle (packages/jabbslad_basic-auth.js:41:53)
W20180530-22:00:41.216(2)? (STDERR) at next (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:174:15)
W20180530-22:00:41.216(2)? (STDERR) at Function.app.handle (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:182:3)
W20180530-22:00:41.216(2)? (STDERR) at Object.fn [as handle] (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:79:14)
W20180530-22:00:41.216(2)? (STDERR) at next (/Users/jirkakrepl/.meteor/packages/webapp/.1.4.0.r7t6p4.pa229++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:174:15)
W20180530-22:00:41.217(2)? (STDERR) at Object.WebAppInternals.staticFilesMiddleware (packages/webapp/webapp_server.js:419:5)
W20180530-22:00:41.217(2)? (STDERR) at Promise.resolve.then (packages/webapp/webapp_server.js:702:23)
W20180530-22:00:41.217(2)? (STDERR) at /Users/jirkakrepl/.meteor/packages/promise/.0.10.0.wb3q6c.q34++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
I don't have more time to play with it, but maybe it would be helpful for someone:)