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

fastify-tokenize

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-tokenize

A fastify plugin to add Tokenize support through a decorator.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
32
increased by88.24%
Maintainers
2
Weekly downloads
 
Created
Source

fastify-tokenize

ko-fi
License CI

An extremely tiny plugin for Fastify for @cyyynthia/tokenize. Allows you to share the same instance of Tokenize on every part of your server.

Also includes compatibility for the fastify-auth plugin for enhanced experience and flexibility in your Fastify server.

Tokenize removes the pain of generating secure tokens and makes it easy to issue and validate tokens in your application.

Install

pnpm i fastify-tokenize
yarn add fastify-tokenize
npm i fastify-tokenize

Usage

This plugin decorates the fastify instance with a tokenize object. This object is an instance of Tokenize initialized with the secret provided.

fastify.register(require('fastify-tokenize'), { secret: 'btw have i told you i use arch' })

fastify-auth compatibility

You can make use of the very flexible fastify-auth to authenticate users, and let fastify-tokenize handle the whole part of authenticating the user. To enable it, just set fastifyAuth to true, and compatibility functions will magically get added.

On successful authentications, fastify-tokenize will decorate the request with the user property. This property can then be used within your app to greet users with their username or perform more specific checks.

It is mandatory to provide a fetchAccount option when registering fastify-tokenize. This method will receive the account ID as unique argument and should the user account (or a promise resolving to a user account). The only required property is lastTokenReset (or last_token_reset) which is used to invalidate tokens generated prior this date.

// We'll assume we use mongodb as our database here.

fastify.register(require('fastify-auth'))
fastify.register(require('fastify-mongodb'), { url: 'mongodb://localhost:27017/my-awesome-db' })
fastify.register(require('fastify-tokenize'), {
  fastifyAuth: true,
  fetchAccount: (userId) => fastify.mongo.db.collection('users').findOne({ _id: userId }),
  secret: 'btw have i told you i use arch'
})

fastify.route({
  method: 'GET',
  url: '/secure-place',
  // fastify.verifyTokenizeToken is added by fastify-tokenize when fastifyAuth is set to "true"
  preHandler: fastify.auth([ fastify.verifyTokenizeToken ]),
  handler: (req, reply) => {
    req.log.info('Auth route')
    reply.send({ hello: 'world' })
  }
})

By default, fastify-tokenize checks for either the token cookie without performing signature checks (will only work if fastify-cookie) is registered, or a token passed in the authorization header. You can obviously customize this for yourself through the following options:

  • Setting cookie to false will disable authentication through cookies. Same thing for header
  • Setting cookie to any string will tell fastify-tokenize to check for this cookie when attempting to authenticate a request
  • You can set cookieSigned to true so fastify-tokenize knows the cookie has to be passed through unsignCookie
  • Setting header to null (default) will attempt to look for a naked token
  • Setting header to any string will tell fastify-tokenize to only look for specific authorization types Example: if you set header to User, it'll look for authorization: User <token>

FAQs

Package last updated on 27 Dec 2020

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