UNPKG

@psteinroe/fastify-supabase

Version:

A Fastify plugin to use authenticated Supabase clients in your API.

116 lines (113 loc) 4.01 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default }); module.exports = __toCommonJS(src_exports); // src/fastify-supabase.ts var import_error = __toESM(require("@fastify/error"), 1); var import_supabase_js = require("@supabase/supabase-js"); var import_fastify_plugin = __toESM(require("fastify-plugin"), 1); var AuthorizationTokenInvalidError = (0, import_error.default)( "FST_SB_AUTHORIZATION_TOKEN_INVALID", "Authorization token is invalid", 401 ); var NoUserDataFoundError = (0, import_error.default)( "FST_SB_NO_USER_DATA_FOUND", "No user data found in the request. Make sure to run request.jwtVerify() before trying to access the user.", 401 ); var fastifySupabase = (fastify, opts, next) => { const { url, serviceKey, anonKey, options } = opts; const supabase = (0, import_supabase_js.createClient)(url, serviceKey, options); if (fastify.supabaseClient) { return next(new Error("fastify-supabase has already been registered")); } fastify.decorate("supabaseClient", supabase); fastify.decorateRequest("_supabaseClient"); fastify.decorateRequest( "supabaseClient", { getter() { const req = this; if (req._supabaseClient) return req._supabaseClient; if (req.user.role === "service_role") { req._supabaseClient = fastify.supabaseClient; } else if (req.user.role && req.user.role !== "anon") { const client = (0, import_supabase_js.createClient)(url, anonKey, { ...options, auth: { ...options?.auth, persistSession: false }, global: { ...options?.global, headers: { ...options?.global?.headers, Authorization: `Bearer ${fastify.jwt.lookupToken(req)}` } } }); req._supabaseClient = client; } if (!req._supabaseClient) { throw new AuthorizationTokenInvalidError(); } return req._supabaseClient; } }, ["user"] ); fastify.decorateRequest( "supabaseUser", { getter() { const req = this; if (!req.user) { throw new NoUserDataFoundError(); } return req.user; } }, ["user"] ); next(); }; var fastify_supabase_default = (0, import_fastify_plugin.default)(fastifySupabase, { fastify: "5.x", name: "fastify-supabase", dependencies: ["@fastify/jwt"] }); // src/index.ts var src_default = fastify_supabase_default; //# sourceMappingURL=index.cjs.map