1.0.1 • Published 3 months ago

@yutai/egg-jwt v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

egg-jwt

Install

$ npm i '@yutai/egg-jwt' --save

Usage

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

Configuration

// {app_root}/config/config.default.js
exports.jwt = {
  secret: '123456', // 自定义 token 的加密条件字符串
  msg: '需要登录后访问', // 自定义 token 401错误信息
  // 正则匹配 /api/auth 开头的 url 都需要验证
  path: /^\/api\/public/, // 不受保护的路由正则
  expiresIn: '1d', // 可选,设置 token 的过期时间
};

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

Example

//共两个方法,一个是签发token,一个是验证token
// 方法一:
const token = ctx.app.jwt.sign({ id: 2, name: '测试用户' }, ctx.app.config.jwt.secret);
// 方法二:
const user = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

// 获取jwt中的用户信息
const user = ctx.state.user;
1.0.1

3 months ago

1.0.0

3 months ago