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

express-jwt

Package Overview
Dependencies
Maintainers
4
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-jwt

JWT authentication middleware.

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
413K
decreased by-7.28%
Maintainers
4
Weekly downloads
 
Created

What is express-jwt?

The express-jwt package is a middleware for Express.js that allows you to validate JSON Web Tokens (JWTs) in your HTTP requests. It is commonly used for securing routes and ensuring that only authenticated users can access certain endpoints.

What are express-jwt's main functionalities?

JWT Validation

This feature allows you to validate JWTs in incoming requests. The middleware checks the token's validity and ensures it is signed with the correct secret key and algorithm.

const express = require('express');
const jwt = require('express-jwt');
const app = express();

const jwtMiddleware = jwt({
  secret: 'your-secret-key',
  algorithms: ['HS256']
});

app.use('/protected', jwtMiddleware, (req, res) => {
  res.send('This is a protected route');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Error Handling

This feature allows you to handle errors that occur during JWT validation. For example, you can send a 401 Unauthorized response if the token is invalid.

const express = require('express');
const jwt = require('express-jwt');
const app = express();

const jwtMiddleware = jwt({
  secret: 'your-secret-key',
  algorithms: ['HS256']
});

app.use('/protected', jwtMiddleware, (err, req, res, next) => {
  if (err.name === 'UnauthorizedError') {
    res.status(401).send('Invalid token');
  } else {
    next(err);
  }
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Token Decoding

This feature allows you to decode the JWT and attach the payload to the request object. You can then access the decoded information in your route handlers.

const express = require('express');
const jwt = require('express-jwt');
const app = express();

const jwtMiddleware = jwt({
  secret: 'your-secret-key',
  algorithms: ['HS256'],
  requestProperty: 'auth'
});

app.use('/protected', jwtMiddleware, (req, res) => {
  res.send(`Hello, ${req.auth.name}`);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to express-jwt

Keywords

FAQs

Package last updated on 16 Mar 2015

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