1.1.4 • Published 6 years ago
egg-full-jwt v1.1.4
egg-full-jwt
Chinese 中文
Important
jsonwebtoken@8.4.0
Install
$ npm i egg-full-jwt --saveUsage
// {app_root}/config/plugin.js
exports.fullJwt = {
  enable: true,
  package: 'egg-full-jwt',
};Configuration
// {app_root}/config/config.default.js
exports.fullJwt = {
  secret: 'your key',
  exp: 'expire time', // default 3600 seconds
};see config/config.default.js for more detail.
Example
'use strict';
module.exports = app => {
  class HomeController extends app.Controller {
    // /login?userId=10001
    async login() {
      // password check ...
      const { userId } = this.ctx.query;
      const token = await app.fullJwt.sign({ userId });
      // cookies
      this.ctx.cookies.set('token', token, {
        maxAge: 24 * 3600 * 1000 * 1,
        httpOnly: true,
      });
      this.ctx.body = 'login success';
    }
    async index() {
      const token = this.ctx.cookies.get('token');
      if (token === false) {
        this.ctx.redirect('/login');
      }
      const { userId } = await app.fullJwt.verify(token);
      if (userId) {
        this.ctx.redirect('/login');
      }
      this.ctx.body = 'hello World';
    }
  }
  return HomeController;
};