Coder Social home page Coder Social logo

egg-passport-jwt's Introduction

egg-passport-jwt

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

Install

$ npm i egg-passport-jwt --save

Usage

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

Configuration

// {app_root}/config/config.default.js
exports.passportJwt = {
  secret: 'your jwt secret or key',
};

see passport-jwt for more detail.

Example

Authenticate requests

Use app.passport.authenticate() specifying 'jwt' as the strategy.

// app/router.js
module.exports = app => {
  const { router, controller } = app;
  const jwt = app.passport.authenticate('jwt', { session: false, successReturnToOrRedirect: null });

  router.get('/', controller.home.index);
  router.get('/protected', jwt, controller.home.index);
};

Include the JWT in requests

The method of including a JWT in a request depends entirely on the extractor function you choose. For example, if you use the fromAuthHeaderAsBearerToken extractor (default), you would include an Authorization header in your request with the scheme set to bearer. e.g.

Authorization: bearer JSON_WEB_TOKEN_STRING...

Verify and store user

Use app.passport.verify(async (ctx, user) => {}) hook:

// app.js
module.exports = app => {
  app.passport.verify(async (ctx, user) => {
    // check user
    assert(user.provider, 'user.provider should exists');
    assert(user.payload, 'user.payload should exists');

    // find user from database
    const existsUser = await ctx.model.User.findOne({ id: user.payload.sub });
    if (existsUser) return existsUser;

    // or you could create a new user
  });
};

Questions & Suggestions

Please open an issue here.

License

MIT

egg-passport-jwt's People

Contributors

chunkai1312 avatar shawnliujw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.