Socket
Socket
Sign inDemoInstall

@fastify/jwt

Package Overview
Dependencies
Maintainers
20
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/jwt

JWT utils for Fastify


Version published
Weekly downloads
140K
increased by0.07%
Maintainers
20
Weekly downloads
 
Created

What is @fastify/jwt?

@fastify/jwt is a Fastify plugin that provides JSON Web Token (JWT) authentication and authorization functionalities. It allows you to sign, verify, and decode JWTs easily within a Fastify application.

What are @fastify/jwt's main functionalities?

Sign a JWT

This feature allows you to sign a JWT with a payload. The secret key is used to sign the token, ensuring its integrity and authenticity.

const fastify = require('fastify')();
const fastifyJwt = require('@fastify/jwt');

fastify.register(fastifyJwt, { secret: 'supersecret' });

fastify.post('/sign', (request, reply) => {
  const token = fastify.jwt.sign({ payload: 'data' });
  reply.send({ token });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Verify a JWT

This feature allows you to verify a JWT. The token is extracted from the Authorization header and verified using the secret key.

const fastify = require('fastify')();
const fastifyJwt = require('@fastify/jwt');

fastify.register(fastifyJwt, { secret: 'supersecret' });

fastify.get('/verify', (request, reply) => {
  try {
    const token = request.headers.authorization.split(' ')[1];
    const decoded = fastify.jwt.verify(token);
    reply.send(decoded);
  } catch (err) {
    reply.send(err);
  }
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Decode a JWT

This feature allows you to decode a JWT without verifying its signature. This can be useful for extracting the payload data from the token.

const fastify = require('fastify')();
const fastifyJwt = require('@fastify/jwt');

fastify.register(fastifyJwt, { secret: 'supersecret' });

fastify.get('/decode', (request, reply) => {
  const token = request.headers.authorization.split(' ')[1];
  const decoded = fastify.jwt.decode(token);
  reply.send(decoded);
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/jwt

Keywords

FAQs

Package last updated on 07 Aug 2024

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