Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Created May 2, 2014 03:32
Show Gist options
  • Select an option

  • Save marcusoftnet/47cf174aa9ba263ee459 to your computer and use it in GitHub Desktop.

Select an option

Save marcusoftnet/47cf174aa9ba263ee459 to your computer and use it in GitHub Desktop.
koa-basic-auth example
var auth = require('koa-basic-auth');
var koa = require('koa');
var app = koa();
// custom 401 handling
app.use(function *(next){
try {
yield next;
} catch (err) {
if (401 == err.status) {
this.status = 401;
this.set('WWW-Authenticate', 'Basic');
this.body = 'cant haz that';
} else {
throw err;
}
}
});
// require auth
app.use(auth({ name: 'tj', pass: 'tobi' }));
// secret response
app.use(function *(){
this.body = 'secret';
});
app.listen(3000);
console.log('listening on port 3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment