# koa-basic-auth

> Blanket basic auth middleware for koa

Latest version **4.0.0** (published 2019-01-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-basic-auth
pnpm add koa-basic-auth
yarn add koa-basic-auth
bun add koa-basic-auth
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2019-01-07 |
| First published | 2013-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/koa-basic-auth) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 3.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 138 |
| Maintainers | aheckmann, coderhaoxin, dead_horse, eivifj, fengmk2, jongleberry, juliangruber, niftylettuce, tjholowaychuk |
| Keywords | koa, auth, authentication, basicauth, basic auth |

## Links

- npm: https://www.npmjs.com/package/koa-basic-auth
- Repository: https://github.com/koajs/basic-auth
- Homepage: https://github.com/koajs/basic-auth#readme
- Issues: https://github.com/koajs/basic-auth/issues
- npm.io page: https://npm.io/package/koa-basic-auth

## Dependencies (2)

- [tsscmp](https://npm.io/package/tsscmp.md) ^1.0.6
- [basic-auth](https://npm.io/package/basic-auth.md) ^2.0.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2019-01-07
- 2.0.0 (next) — 2016-03-18
- 3.0.0 — 2018-05-16
- 1.1.2 — 2014-09-15
- 1.1.1 — 2014-01-19
- 1.1.0 — 2013-11-30
- 1.0.0 — 2013-08-20

## README

# koa-basic-auth [![Build Status](https://travis-ci.org/koajs/basic-auth.png)](https://travis-ci.org/koajs/basic-auth)

  Add simple "blanket" basic auth with username / password. If you require
  anything more specific just use the [basic-auth](https://github.com/visionmedia/node-basic-auth) module.

  **v4.x+ Breaking Change:** This package no longer requires both a username and a password.  Either or is supported, see [#39](https://github.com/jshttp/basic-auth/issues/39) for more insight.

## Installation

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

## Example

  Password protect downstream middleware:

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

// custom 401 handling
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    if (401 == err.status) {
      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(async (ctx) => {
  ctx.body = 'secret';
});

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

  Example request:

    $ curl -H "Authorization: basic dGo6dG9iaQ==" http://localhost:3000/ -i
    HTTP/1.1 200 OK
    X-Powered-By: koa
    Content-Type: text/plain; charset=utf-8
    Content-Length: 6
    Date: Sat, 30 Nov 2013 19:35:17 GMT
    Connection: keep-alive

    secret

 Using the [mount](https://github.com/koajs/mount) middleware you may specify auth for a given prefix:

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

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

## License

  MIT

---
_Source: https://npm.io/package/koa-basic-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
