JSPM

passport-jwt-mock

0.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 188
  • Score
    100M100P100Q106032F
  • License MIT

Mock strategy for testing JWT authentication with `passport`.

Package Exports

  • passport-jwt-mock

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (passport-jwt-mock) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

passport-mock

Mock passport JWT strategy for testing a Node.js application.

This module allows you authenticate a fake user in a Node application using your existing passport-jwt implementation to allow for easy testing of routes.

Install

Install using yarn:

$ yarn add passport-jwt-mock --dev

Or use npm if you wish:

$ npm install passport-jwt-mock --save-dev

Usage

To get started you can import MockStrategy and create a new instance. I believe the easiest approach is to use an environment variable to hot-swap the regular jwtStrategy with the mock strategy.

const passport = require("passport");

let jwtStrategy = null;
if (process.env.NODE_ENV === "test") {
    jwtStrategy = require("passport-jwt-mock").Strategy;
} else {
    jwtStrategy = require("passport-jwt").Strategy;
}

Once you have replaced the the jwtStrategy object, you are nearly done! The implementation (however you used it) will work without modifying the implementation.

passport.use(
    new jwtStrategy(
        {
            //secret we used to sign our JWT
            secretOrKey: 'secret',
            //we expect the user to send the token as a query paramater with the name 'secret_token'
            jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken()
        },
        async (token, done) => {
            try {
                //Pass the user details to the next middleware
                return done(null, token.user);
            } catch (error) {
                done(error);
            }
        }
    )
);

Important Note

This package is for testing purposes only! Please do not use this for actual authentication as it provides no security whatsoever!

License

passport-mock-strategy is available under the MIT License.

Contributing

Contributions are welcome. Feel free to open an issue or submit a pull request.