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

passport-jwt

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

passport-jwt

Passport authentication strategy using JSON Web Tokens

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
increased by6.86%
Maintainers
1
Weekly downloads
 
Created

What is passport-jwt?

The passport-jwt npm package is a Passport strategy for authenticating with JSON Web Tokens (JWT). It allows you to secure your endpoints and authenticate requests using JWTs, which are often used in stateless authentication mechanisms.

What are passport-jwt's main functionalities?

JWT Authentication Strategy

This feature allows you to set up a JWT authentication strategy using Passport. The code sample demonstrates how to configure the strategy with options such as extracting the JWT from the authorization header and specifying the secret key. The strategy then verifies the token and retrieves the user information.

const passport = require('passport');
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');

const opts = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: 'your_jwt_secret'
};

passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
  User.findById(jwt_payload.sub, (err, user) => {
    if (err) {
      return done(err, false);
    }
    if (user) {
      return done(null, user);
    } else {
      return done(null, false);
    }
  });
}));

Protecting Routes

This feature allows you to protect specific routes in your application. The code sample shows how to use the `passport.authenticate` middleware to secure the `/protected` route, ensuring that only authenticated users with a valid JWT can access it.

const express = require('express');
const passport = require('passport');

const app = express();

app.get('/protected', passport.authenticate('jwt', { session: false }), (req, res) => {
  res.json({ message: 'You have accessed a protected route!', user: req.user });
});

Other packages similar to passport-jwt

Keywords

FAQs

Package last updated on 24 Dec 2022

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