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

@forrestjs/service-hasura-auth

v5.2.2

Published

ForrestJS service which helps running an Hasura Webhook authentication service

Downloads

3,193

Readme

Fastify Hasura Auth

Adds Hasura Auth webhook compatible APIs.

With default settings you should configure your Hasura instance as:

HASURA_GRAPHQL_AUTH_HOOK=http://your-service.com/hasura-auth
HASURA_GRAPHQL_AUTH_HOOK_MODE=POST

Change the Default Prefix

const serviceFastify = require("@forrestjs/service-fastify");
const serviceHasuraAuth = require("@forrestjs/service-hasura-auth");

forrestjs.run({
  settings: {
    hasuraAuth: {
      // this is the default value
      prefix: "/hasura-auth"
    }
  },
  services: [serviceFastify, serviceHasuraAuth]
});

Setup a GET handler

The following example let you add a validation method to an Hasura GET Webhook:

const serviceFastify = require("@forrestjs/service-fastify");
const serviceHasuraAuth = require("@forrestjs/service-hasura-auth");

forrestjs.run({
  services: [serviceFastify, serviceHasuraAuth],
  features: [
    {
      target: "$HASURA_AUTH_GET",
      handler: {
        validate: async (request, reply) => {
          const userId = request.headers["x-user-id"];

          if (!userId) {
            throw new Error("User ID not found");
          }

          request.hasuraClaims.push("role", "user");
          request.hasuraClaims.push("user-id", userId);
        }
      }
    }
  ]
});

Setup a POST handler

The following example let you add a validation method to an Hasura POST Webhook:

const serviceFastify = require("@forrestjs/service-fastify");
const serviceHasuraAuth = require("@forrestjs/service-hasura-auth");

forrestjs.run({
  services: [serviceFastify, serviceHasuraAuth],
  features: [
    {
      target: "$HASURA_AUTH_POST",
      handler: {
        validate: async (request, reply) => {
          const userId = request.body.headers["x-user-id"];

          if (!userId) {
            throw new Error("User ID not found");
          }

          request.hasuraClaims.push("role", "user");
          request.hasuraClaims.push("user-id", userId);
        }
      }
    }
  ]
});

Please refer to Hasura POST Webhook documentation to figure out what's inside the request object.

The validate() Function

Use the validate() function to decorate your request with Hasura's claims:

request.hasuraClaims.push("role", "user");

or block the request by throwing a simple Javascript error:

throw new Error("Thy shall not pass!");
  • any claim you add will be serialized and automatically prefixed with x-hasura-{yourClaim} for your convenience
  • any Error will produce a 401 response status code accordingly to Hasura's specs

Configure your Route

When extending $HASURA_AUTH_GET or $HASURA_AUTH_POST you can pass all the Fastify's Route params that will be simply proxied to Fastify:

const myExtension = {
  target: "$HASURA_AUTH_POST",
  handler: {
    // Hasura Auth API:
    validate: (request, reply) => {},

    // Fastify API:
    preHandler: (request, reply) => {},
    schema: {}
  }
};

NOTE: You can not change method and handler, and url is defaulted to / but you can override it.