koa-auth-header-parser
v1.0.0
Published
Yet another very basic auth header parser
Downloads
5
Maintainers
Readme
koa-auth-header-parser
Yet another very basic auth header parser for koa < 2
Usage
const koa = require('koa');
const authHeaderParser = require('koa-auth-header-parser');
const app = koa();
app.use(authHeaderParser());
app.use(function *() {
const authToken = this.state.authToken;
const authType = this.state.authType;
this.assert(authType === 'Bearer', 400);
const user = yield User.find({ token: authToken });
this.assert(user, 403);
this.body = `Hello ${ user.name }`
});
API
authHeaderParser(opts);
| Param | Type | Description | | --- | --- | --- | | [opts] | Object | | | [opts.handlers] | Object | custom handlers | | [opts.handlers.[name]] | Function | custom handlers |
Advanced usage
app.use(authHeaderParser({
handlers: {
Bearer: function * (token) {
this.state.user = yield User.find({ token });
this.assert(this.state.user, 403);
},
default: function * () {
this.throw(400, 'Unknown auth method');
}
}
}));