1.0.0 • Published 8 years ago

koa-auth-header-parser v1.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
8 years ago

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);
ParamTypeDescription
optsObject
opts.handlersObjectcustom handlers
[opts.handlers.name]Functioncustom 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');
        }
    }
}));