Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

egg-passport-jwt

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-passport-jwt

jwt passport plugin for egg

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 30 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc