@dazejs/jwt-provider
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

GitHub issues npm npm actions codecov GitHub license

Jwt Provider

基于 Daze.js 的 jwt 扩展

安装

$ npm i --save @dazejs/jwt-provider

使用

首先在 /src/config/app.ts 将提供者注册到框架中:

import { JwtServiceProvider } from '@dazejs/jwt-provider';

export default {
  // ...
  providers: [
    // ...
    JwtServiceProvider.configure({
      secret: 'hard!to-guess_secret',
      privateKey: '...',
      publicKey: '...',
      algorithm: 'HS256',
    })
  ]
}

configure 方法用来配置 JwtServiceProvider, 参数支持:

  • secret: HMAC / PEM 加密需要配置该参数
  • publicKey / privateKey: RSA / ECDSA 加密所需的公钥和私钥
  • algorithm: 默认所使用的加密算法,详细可以查看 crypto

使用 @inject 进行注入即可使用

import { Controller, route, http, inject } from '@dazejs/framework'
import { Jwt } from '@dazejs/jwt-provider'

@route('users')
export class User extends Controller {

    @inject() jwt: Jwt;

    // get /users
    @http.het()
    index() {
        const token = this.jwt.sign({
          uid: 1
        })
        return token
    }
}

Jwt

Jwt 是基于 jsonwebtoken 实现的

sign(payload: string | Object | Buffer, options?: SignOption): string

该方法基于 jsonwebtokensign 方法实现

verify(token: string, options?: VerifyOption): any

该方法基于 jsonwebtokenverify 方法实现

decode(token: string, options: DecodeOptions): object | string

该方法基于 jsonwebtokendecode 方法实现

Package Sidebar

Install

npm i @dazejs/jwt-provider

Weekly Downloads

7

Version

2.0.0

License

MIT

Unpacked Size

25.9 kB

Total Files

22

Last publish

Collaborators

  • zewail