UNPKG

fastify-auth0-verify

Version:
53 lines (40 loc) 1.53 kB
'use strict' const fastifyPlugin = require('fastify-plugin') const fastifyJwtJwks = require('fastify-jwt-jwks').fastifyJwtJwks const errorMessages = { missingOptions: 'Please provide at least one of the "domain" or "secret" options.' } function fastifyAuth0Verify(instance, options, done) { try { let { domain, secret } = options if (domain) { delete options.domain domain = domain.toString() if (!domain.match(/^http(?:s?)/)) { domain = new URL(`https://${domain}`).toString() } else { // Add missing trailing slash if it hasn't been provided in the config domain = new URL(domain).toString() } options.jwksUrl = `${domain}.well-known/jwks.json` } else if (!secret) { // Throw missing options error here to prevent confusing error // message from fastify-jwt-jwks being thrown to user throw new Error(errorMessages.missingOptions) } return fastifyJwtJwks(instance, options, function (e) { if (e) { return done(e) } instance.decorate('auth0Verify', instance.jwtJwks) done() }) } catch (e) { done(e) } } module.exports = fastifyPlugin(fastifyAuth0Verify, { name: 'fastify-auth0-verify', fastify: '5.x' }) // Set the default export to the fastifyAuth0Verify function for ES module compatibility module.exports.default = fastifyAuth0Verify // Add a named export for the fastifyAuth0Verify function for CommonJS compatibility module.exports.fastifyAuth0Verify = fastifyAuth0Verify