https://github.com/waitingsong/egg-jwt
egg-plugin jwt
https://github.com/waitingsong/egg-jwt
authentication egg jwt middleware midway sign typescript verify
Last synced: about 2 months ago
JSON representation
egg-plugin jwt
- Host: GitHub
- URL: https://github.com/waitingsong/egg-jwt
- Owner: waitingsong
- License: mit
- Created: 2019-08-21T03:09:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-05T04:40:10.000Z (over 4 years ago)
- Last Synced: 2024-10-11T17:28:53.704Z (about 1 year ago)
- Topics: authentication, egg, jwt, middleware, midway, sign, typescript, verify
- Language: TypeScript
- Size: 332 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# [egg-jwt](https://waitingsong.github.io/egg-jwt/)
[jwt](https://www.npmjs.com/package/@waiting/egg-jwt) siging,
verifying and authentication for midway/egg framework.
[](https://www.npmjs.com/package/@waiting/egg-jwt)
[](https://opensource.org/licenses/MIT)

[](https://github.com/waitingsong/egg-jwt/actions?query=workflow%3A%22ci%22)
[](https://codecov.io/gh/waitingsong/egg-jwt)
[](https://conventionalcommits.org)
## Installation
```sh
npm install @waiting/egg-jwt
```
## Configuration
### Enable Plugin
Edit `${app_root}/src/config/plugin.ts`:
```ts
export const jwt = {
enable: true,
package: '@waiting/egg-jwt',
}
```
### Add Configurations
```ts
/* location: ${app_root}/src/config/config.${env}.ts */
import { JwtEggConfig } from '@waiting/egg-jwt'
export const jwt: JwtEggConfig = {
agent: false,
enable: true, // enable middleware
client: {
debug: false,
secret: '123456abc',
},
// https://github.com/eggjs/egg-path-matching
ignore: ['/signup', '/login'],
}
```
## Usage
```ts
import { Provide, Plugin } from '@midwayjs/decorator'
import { Jwt } from '@waiting/egg-jwt'
import assert from 'assert'
@provide()
export class UserService {
constructor(
@plugin() private readonly jwt: Jwt,
) { }
@get('/siginup')
public signup(ctx: Context) {
const payload = { foo: 'bar', iat: 1566629919 }
const token = this.jwt.sign(payload)
const valid = this.jwt.verify(token)
assert.deepStrictEqual(valid, payload)
ctx.body = `\nToken: ${token}`
}
@get('/')
public index(ctx: Context): void {
ctx.body = `\nPayload: ${ctx.jwtState && ctx.jwtState.user ? JSON.stringify(ctx.jwtState.user) : 'Not found'}`
}
}
```
Then:
```sh
curl -I 127.0.0.1:7001
// response HTTP/1.1 401 Unauthorized
curl 127.0.0.1:7001/signup
// response ends with signature 'PZkACzct30IcrymoodYlW0LW0Fc1r6Hs1l8yOZSeNpk'
curl 127.0.0.1:7001/ \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1NjY2Mjk5MTl9.PZkACzct30IcrymoodYlW0LW0Fc1r6Hs1l8yOZSeNpk"
// response payload: {"foo":"bar","iat":1566629919}
```
## License
[MIT](LICENSE)
### Languages
- [English](README.md)
- [中文](README.zh-CN.md)