1.1.4 • Published 5 years ago

egg-full-jwt v1.1.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

egg-full-jwt

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Chinese 中文

Important

jsonwebtoken@8.4.0

Install

$ npm i egg-full-jwt --save

Usage

// {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;
};

License

MIT

1.1.4

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago