0.0.4 • Published 5 years ago

@lvchengbin/koa-basic-auth v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

koa-basic-auth

A middleware for koa to add http basic authentication.

Installation

$ npm i @lvchengbin/koa-basic-auth --save

Usage

const Koa = require( 'koa' );
const auth = require( '@lvchengbin/koa-basic-auth' );

const app = new Koa();

// throw 401 if authentication failed
app.use( auth( 'username', 'password' ) );

app.listen( 3000 );

To use with a callback function:

app.use( auth( 'username', 'password', ( result, ctx, next ) => {
    if( !result ) {
        ctx.body = 'Unauthorized';
    } else {
        next();
    }
} );

app.use( async ctx => {
    ctx.body = 'yep';
} );

To use the function as a ordinary function:

const Koa = require( 'koa' );
const Router = require( '@lvchengbin/koa-router' );
const auth = require( '@lvchengbin/koa-basic-auth' );

const app = new Koa();
const router = new Router( app );

router.get( '/', async ctx => {
    if( auth( 'username', 'password', ctx ) ) {
        ctx.body = 'yes';
    } else {
        ctx.body = 'no';
    }
} );

app.listen( 3000 );
0.0.4

5 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago