# egg-jwt

> JWT authentication plugin for egg

Latest version **3.1.7** (published 2019-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install egg-jwt
pnpm add egg-jwt
yarn add egg-jwt
bun add egg-jwt
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.7 |
| Published | 2019-11-19 |
| First published | 2016-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 2 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 273 |
| Author | okoala |
| Maintainers | dapixp |
| Keywords | egg, eggPlugin, egg-plugin, jwt |

## Links

- npm: https://www.npmjs.com/package/egg-jwt
- Repository: https://github.com/okoala/egg-jwt
- Homepage: https://github.com/okoala/egg-jwt#readme
- Issues: https://github.com/okoala/egg-jwt/issues
- npm.io page: https://npm.io/package/egg-jwt

## Dependencies (2)

- [koa-jwt2](https://npm.io/package/koa-jwt2.md) ^1.0.3
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.3.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

- 3.1.7 (latest) — 2019-11-19
- 3.1.6 — 2019-02-21
- 3.1.5 — 2019-01-29
- 3.1.4 — 2018-10-16
- 3.1.3 — 2018-10-12
- 3.1.2 — 2018-08-26
- 3.1.1 — 2018-08-07
- 3.1.0 — 2018-08-03
- 3.0.0 — 2018-03-13
- 2.2.1 — 2018-01-16
- 2.2.0 — 2017-09-20
- 2.1.0 — 2017-06-26
- 2.0.0 — 2017-05-27
- 1.1.0 — 2016-10-08
- 0.1.1 — 2016-09-30

## README

# egg-jwt

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-jwt.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-jwt
[travis-image]: https://img.shields.io/travis/okoala/egg-jwt.svg?style=flat-square
[travis-url]: https://travis-ci.org/okoala/egg-jwt
[codecov-image]: https://img.shields.io/codecov/c/github/okoala/egg-jwt.svg?style=flat-square
[codecov-url]: https://codecov.io/github/okoala/egg-jwt?branch=master
[david-image]: https://img.shields.io/david/okoala/egg-jwt.svg?style=flat-square
[david-url]: https://david-dm.org/okoala/egg-jwt
[snyk-image]: https://snyk.io/test/npm/egg-jwt/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-jwt
[download-image]: https://img.shields.io/npm/dm/egg-jwt.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-jwt

Egg's JWT(JSON Web Token Authentication Plugin)

## Important

> egg-jwt@3 use koa-jwt2

## Install

```bash
$ npm i egg-jwt --save
```

or

```
yarn add egg-jwt
```

## Usage

```js
// {app_root}/config/plugin.js
exports.jwt = {
  enable: true,
  package: "egg-jwt"
};
```

## Configuration

```js
// {app_root}/config/config.default.js
exports.jwt = {
  secret: "123456"
};
```

see [config/config.default.js](config/config.default.js) for more detail.

## Example

```javascript
// app/router.js
"use strict";

module.exports = app => {
  app.get("/", app.jwt, "render.index"); // use old api app.jwt
  app.get("/login", "login.index");
  app.get("/success", "success.index"); // is setting in config.jwt.match
};

// app/controller/render.js
("use strict");

module.exports = app => {
  class RenderController extends app.Controller {
    *index() {
      this.ctx.body = "hello World";
    }
  }
  return RenderController;
};

// app/controller/login.js
("use strict");

module.exports = app => {
  class LoginController extends app.Controller {
    *index() {
      this.ctx.body = "hello admin";
    }
  }
  return LoginController;
};

// app/controller/success.js
("use strict");

module.exports = app => {
  class SuccessController extends app.Controller {
    *index() {
      this.ctx.body = this.ctx.state.user;
    }
  }
  return SuccessController;
};
```

Then

```
curl 127.0.0.1:7001
// response 401

curl 127.0.0.1:7001/login
// response hello admin

curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0OTAwMTU0MTN9.ehQ38YsRlM8hDpUMKYq1rHt-YjBPSU11dFm0NOroPEg" 127.0.0.1:7001/success
// response {foo: bar}
```

## How To Create A Token

```
const token = app.jwt.sign({ foo: 'bar' }, app.config.jwt.secret);
```

For more options, check [here](https://github.com/auth0/node-jsonwebtoken)

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](LICENSE)

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