Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/okoala/egg-jwt
JWT authentication plugin for egg
https://github.com/okoala/egg-jwt
Last synced: 5 days ago
JSON representation
JWT authentication plugin for egg
- Host: GitHub
- URL: https://github.com/okoala/egg-jwt
- Owner: okoala
- License: mit
- Created: 2016-09-30T10:31:46.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-03-23T13:23:05.000Z (over 2 years ago)
- Last Synced: 2024-03-21T03:03:05.024Z (8 months ago)
- Language: JavaScript
- Size: 220 KB
- Stars: 272
- Watchers: 6
- Forks: 33
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-jwt - JWT authentication plugin for egg. ![](https://img.shields.io/github/stars/okoala/egg-jwt.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-jwt.svg?style=flat-square) (仓库 / 插件)
- awesome-hacking-lists - okoala/egg-jwt - JWT authentication plugin for egg (JavaScript)
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-jwtEgg'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 401curl 127.0.0.1:7001/login
// response hello admincurl -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)