Created
May 2, 2014 03:32
-
-
Save marcusoftnet/47cf174aa9ba263ee459 to your computer and use it in GitHub Desktop.
koa-basic-auth example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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