@forrestjs/service-hasura-auth
v5.2.2
Published
ForrestJS service which helps running an Hasura Webhook authentication service
Downloads
3,193
Maintainers
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
andhandler
, andurl
is defaulted to/
but you can override it.