npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-tokenize

v1.4.0

Published

A fastify plugin to add Tokenize support through a decorator.

Downloads

93

Readme

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
  }
}