• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JWT authentication plugin for egg

egg-jwt

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

Egg's JWT(JSON Web Token Authentication Plugin)

Important

egg-jwt@3 use koa-jwt2

Install

$ npm i egg-jwt --save

or

yarn add egg-jwt

Usage

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

Configuration

// {app_root}/config/config.default.js
exports.jwt = {
  secret: "123456"
};

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

Example

// app/router.js
"use strict";

module.exports = app => {
  app.get("/", app.jwt, "render.index"); // use old api app.jwt
  app.get("/login", "login.index");
  app.get("/success", "success.index"); // is setting in config.jwt.match
};

// app/controller/render.js
("use strict");

module.exports = app => {
  class RenderController extends app.Controller {
    *index() {
      this.ctx.body = "hello World";
    }
  }
  return RenderController;
};

// app/controller/login.js
("use strict");

module.exports = app => {
  class LoginController extends app.Controller {
    *index() {
      this.ctx.body = "hello admin";
    }
  }
  return LoginController;
};

// app/controller/success.js
("use strict");

module.exports = app => {
  class SuccessController extends app.Controller {
    *index() {
      this.ctx.body = this.ctx.state.user;
    }
  }
  return SuccessController;
};

Then

curl 127.0.0.1:7001
// response 401

curl 127.0.0.1:7001/login
// response hello admin

curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0OTAwMTU0MTN9.ehQ38YsRlM8hDpUMKYq1rHt-YjBPSU11dFm0NOroPEg" 127.0.0.1:7001/success
// response {foo: bar}

How To Create A Token

const token = app.jwt.sign({ foo: 'bar' }, app.config.jwt.secret);

For more options, check here

Questions & Suggestions

Please open an issue here.

License

MIT

More Repositories

1

vue-antd

Vue UI Component & Ant.Design
CSS
1,716
star
2

vue-fis3

Vue & Fis3 & Use Vue Component
JavaScript
256
star
3

egg-vue-typescript-apollo-graphql-startkit

仅供参考,不要直接用于实际项目~
JavaScript
177
star
4

vue-vuex

Vue & Vuex & Express Hot-Reload
JavaScript
152
star
5

react-redux-antd

React & Redux & Ant.Design
JavaScript
135
star
6

xque

[WIP] (unofficial) an yuque app that builded by flutter
Dart
103
star
7

30DaysofReactNative

30 Days 30 React Native Project
JavaScript
87
star
8

awesome-stars

Awesome List of my own!
56
star
9

frontend-resources

平常收集的一些资源
49
star
10

docz-theme-yuque

Yuque theme for docz
TypeScript
25
star
11

egg-dingtalk-robot

钉钉机器人egg插件
JavaScript
23
star
12

hexo-theme-pithiness

the theme for hexo
CSS
20
star
13

webpack-prerender-spa-plugin

Prerender static html for spa application
JavaScript
18
star
14

react-native-starter

React Native + Redux + Code Push @ Android iOS Starter Kit
JavaScript
15
star
15

koa-jwt2

koa middleware that validates a JsonWebToken (JWT) and sync with express/jwt
JavaScript
14
star
16

sublime-win-bak

我的sublime win 备份
Python
10
star
17

fis3-parser-vue

fis3 parser vue
JavaScript
9
star
18

react-native-music

a music app like netease cloud music
JavaScript
8
star
19

remax-wechat-demo

Remax 微信小程序组件 / API / 云开发示例
JavaScript
8
star
20

egg-multiple-static

support multiple static server plugin for egg
JavaScript
7
star
21

vue-antd-docs-loader

vue antd docs webpack loader
JavaScript
7
star
22

react-native-redux-starter

react native redux demo
JavaScript
6
star
23

vue-apollo

Unofficial Vue2 integration for Apollo
JavaScript
5
star
24

backbone-0.9.9-zn

backbone 中文翻译文档
5
star
25

egg-swaggerbox

swagger box for egg
JavaScript
4
star
26

blog

我的博客
4
star
27

scrajs

[WIP] A scalable web crawler framework for Node.js
TypeScript
3
star
28

vueify-sync

vueify sync vueify 的同步版本
JavaScript
2
star
29

egg-neutrino

Neutrino Plugin for Egg.js
JavaScript
2
star
30

neutrino-preset-egg-typescript

Neutrino preset for supporting Egg.js build with typescript
JavaScript
2
star
31

slides

https://okoala.github.io/slides/2016-08-05-typescript/dist/public
JavaScript
2
star
32

egg-webpack-hot-middleware

JavaScript
1
star
33

okoala.github.com

Koala的博客
JavaScript
1
star
34

xbird

An new way to write mobile app
1
star
35

create-react-app

JavaScript
1
star
36

xfish

An new way to write cms by egg & umi & swagger
1
star
37

puppeteer-capture

JavaScript
1
star
38

egg-webpack-dev-middleware

JavaScript
1
star
39

enterprise-egg

[WIP] Egg.js 的企业级开发
1
star
40

pay

don't fork this project
Java
1
star
41

creative-egg

有关 Egg.js 的创意实践
TypeScript
1
star
42

babel-plugin-import-sideeffect

add import sideeffect plugin for babel
JavaScript
1
star
43

egg-pro

A REST and realtime API layer for modern applications build top of egg
1
star