Skip to content

Commit

Permalink
Implement route.must()
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 23, 2012
1 parent e057f65 commit 4050048
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions index.js
Expand Up @@ -836,14 +836,21 @@ Route.prototype.files = function (filepath) {
return this
}

Route.prototype.auth = function (handler) {
if (!handler) return this.authHandler
this.authHandler = handler
return this
Route.prototype.auth = function () {
return this.must('auth', 401)
}

Route.prototype.must = function () {
this._must = Array.prototype.slice.call(arguments)
// must have auth, or else send a 401:
// app.plugin('auth', authHandler)
// app.route('/x').must('auth', 401)
Route.prototype.must = function (thing, orelse) {
orelse = makeHandler(orelse)
return this.on('request', function (req, res) {
var when = req.app._plugins[thing] ? 'plugin:'+thing : 'pluginsDone'
req.on(when, function () {
if (!req[thing]) req.handler = orelse
})
})
return this
}

Expand Down

0 comments on commit 4050048

Please sign in to comment.