npm.io
1.1.4 • Published 7 years ago

egg-full-jwt

Licence
MIT
Version
1.1.4
Deps
1
Size
8 kB
Vulns
3
Weekly
0
Stars
1

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

Keywords