Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@tool-developer/egg-jwt

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tool-developer/egg-jwt

jwt plugin for egg

Source
npmnpm
Version
0.0.3
Version published
Weekly downloads
15
87.5%
Maintainers
1
Weekly downloads
 
Created
Source

@tool-developer/egg-jwt

NPM version

JWT for egg plugin that supports server - and client-side custom authorization failure (return JSON data, or jump to the specified page).

中文

Install

$ npm i @tool-developer/egg-jwt --save

Usage

// {app_root}/config/plugin.js
exports.jwt = {
  enable: true,
  package: '@tool-developer/egg-jwt',
};

generate token, by payload include user id info, like this:

const payload = {
  uid
}
const token = this.app.jwt.sign(payload);

validate token, get user id info

const valid = this.app.jwt.verify(token);
const {uid} = valid;

Configuration

// {app_root}/config/config.default.js
exports.jwt = {
  secret:'',
  // enable middleware,default false
  enableMiddleware:false,
  //
  cookieTokenSet:'auth-token',
  //{[headerAuthorization]:'[headerAuthorizationScheme] [token]'}
  headerAuthorization:'authorization',
  headerAuthorizationScheme:'Bearer',
  //{[headerPassthroughSet]:false}
  headerPassthroughSet:'x-custom-passthrough',
  //
  // sign page route path
  signPageRoutePath:'',
  // 
  authOptions:{
    // passthrough
  },
  //
  // ctx.state.secret
  contextStateSecret:'secret',
  // ctx.state.user
  contextStateUser:'user',
  // jwt.sign options
  signOptions:{
    expiresIn:'2d'
  },
  // jwt.verify options
  verifyOptions:{},
  // ignore route path
  ignore:[]
};

see config/config.default.js for more detail.

authOptions.passthrough

server authorization options

true: 
  return json data, like{code:401,info}

false:
  when set signPageRoutePath, to redirect signPageRoutePath.
  no set signPageRoutePath, to throw.

/a/b/c:
  to redirect the url.

client authorization options, by headerPassthroughSet, same as the authOptions.

signOptions

signOptions, to see jsonwebtoken jwt.sign

signOptions.expiresIn token expired time, default 2h, to see zeit/ms

verifyOptions

verifyOptions, to see jsonwebtoken jwt.verify

ignore/match

to see egg math and ignore

Example

Questions & Suggestions

Please open an issue here.

License

MIT

Keywords

egg

FAQs

Package last updated on 20 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts