Skip to content

cyyynthia/fastify-tokenize

Repository files navigation

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>

Usage with TypeScript

You can type the request.user field just like Fastify lets you type the querystring and various other request metadata:

import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
import type { User } from './models'

async function process (request: FastifyRequest<{ TokenizeUser: User }>, reply: FastifyReply) {
  if ('user' in request && request.user) {
    // typeof request.user is User
  }
}

About

A fastify plugin to add Tokenize support through a decorator.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published