Skip to content

vanwalj/koa-auth-header-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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');
        }
    }
}));

About

Yet another very basic auth header parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published