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

fastify-jwt

Package Overview
Dependencies
Maintainers
7
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-jwt - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

8

jwt.js

@@ -7,2 +7,6 @@ 'use strict'

const steed = require('steed')
const {
BadRequest,
Unauthorized
} = require('http-errors')

@@ -125,7 +129,7 @@ function wrapStaticSecretInCallback (secret) {

if (!/^Bearer$/i.test(scheme)) {
return next(new Error('Format is Authorization: Bearer [token]'))
return next(new BadRequest('Format is Authorization: Bearer [token]'))
}
}
} else {
return next(new Error('No Authorization was found in request.headers'))
return next(new Unauthorized('No Authorization was found in request.headers'))
}

@@ -132,0 +136,0 @@

{
"name": "fastify-jwt",
"version": "0.3.0",
"version": "0.4.0",
"description": "JWT utils for Fastify",

@@ -30,11 +30,12 @@ "main": "jwt.js",

"dependencies": {
"fastify-plugin": "^1.0.1",
"jsonwebtoken": "^8.2.0",
"fastify-plugin": "^1.2.0",
"http-errors": "^1.7.1",
"jsonwebtoken": "^8.3.0",
"steed": "^1.1.3"
},
"devDependencies": {
"fastify": "^1.4.0",
"standard": "^11.0.1",
"tap": "^11.1.4"
"fastify": "^1.11.2",
"standard": "^12.0.1",
"tap": "^12.0.1"
}
}
# fastify-jwt
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/fastify-jwt.svg?branch=master)](https://travis-ci.org/fastify/fastify-jwt)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/fastify-jwt.svg?branch=master)](https://travis-ci.org/fastify/fastify-jwt) [![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-jwt.svg)](https://greenkeeper.io/)

@@ -32,2 +32,67 @@ JWT utils for Fastify, internally uses [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken).

For verifying & accessing the decoded token inside your services, you can use a global `preHandler` hook to define the verification process like so:
```js
const fastify = require('fastify')
fastify.register(require('fastify-jwt'), {
secret: 'supersecret'
})
fastify.addHook("preHandler", async (request, reply) => {
try {
await request.jwtVerify()
} catch (err) {
reply.send(err)
}
})
```
Aftewards, just use `request.user` in order to retrieve the user information:
```js
module.exports = async function(fastify, opts) {
fastify.get("/", async function(request, reply) {
return request.user
})
}
```
However, most of the time we want to protect only some of the routes in our application. To achieve this you can wrap your authentication logic into a plugin like
```js
const fp = require("fastify-plugin")
module.exports = fp(async function(fastify, opts) {
fastify.register(require("fastify-jwt"), {
secret: "supersecret"
})
fastify.decorate("authenticate", async function(request, reply) {
try {
await request.jwtVerify()
} catch (err) {
reply.send(err)
}
})
})
```
Then use the `beforeHandler` of a route to protect it & access the user information inside:
```js
module.exports = async function(fastify, opts) {
fastify.get(
"/",
{
beforeHandler: [fastify.authenticate]
},
async function(request, reply) {
return request.user
}
)
}
```
Make sure that you also check [fastify-auth](https://github.com/fastify/fastify-auth) plugin for composing more complex strategies.
## API Spec

@@ -34,0 +99,0 @@

@@ -277,3 +277,3 @@ 'use strict'

t.test('no authorization header error', function (t) {
t.plan(1)
t.plan(2)

@@ -286,2 +286,3 @@ fastify.inject({

t.is(error.message, 'No Authorization was found in request.headers')
t.is(response.statusCode, 401)
})

@@ -291,3 +292,3 @@ })

t.test('authorization header format error', function (t) {
t.plan(1)
t.plan(2)

@@ -303,2 +304,3 @@ fastify.inject({

t.is(error.message, 'Format is Authorization: Bearer [token]')
t.is(response.statusCode, 400)
})

@@ -305,0 +307,0 @@ })

Sorry, the diff of this file is not supported yet

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