Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
* Update example and install

* fix code style

* fix code style2

PR #13
  • Loading branch information
xJeneKx authored and haoxin committed Nov 10, 2016
1 parent c6896b0 commit 72696b3
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,44 @@
## Installation

```js
$ npm install koa-basic-auth
$ npm install koa-basic-auth@next
```

## Example

Password protect downstream middleware:

```js
var auth = require('koa-basic-auth');
var koa = require('koa');
var app = koa();
const auth = require('koa-basic-auth');
const Koa = require('koa');
const app = new Koa();

// custom 401 handling

app.use(function *(next){
app.use(async (ctx, next) => {
try {
yield next;
await next();
} catch (err) {
if (401 == err.status) {
this.status = 401;
this.set('WWW-Authenticate', 'Basic');
this.body = 'cant haz that';
ctx.status = 401;
ctx.set('WWW-Authenticate', 'Basic');
ctx.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.use(async (ctx) => {
ctx.body = 'secret';
});

app.listen(3000);
console.log('listening on port 3000');
app.listen(3000, function () {
console.log('listening on port 3000');
});
```

Example request:
Expand All @@ -65,8 +63,8 @@ secret
Using the [mount](https://github.com/koajs/mount) middleware you may specify auth for a given prefix:

```js
var mount = require('koa-mount');
var auth = require('koa-basic-auth');
const mount = require('koa-mount');
const auth = require('koa-basic-auth');

app.use(mount('/admin', auth({ name: 'tobi', pass: 'ferret' })));
```
Expand Down

0 comments on commit 72696b3

Please sign in to comment.