UNPKG

@updatedev/ssr

Version:
94 lines 4.21 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var createServerClient_exports = {}; __export(createServerClient_exports, { createServerClient: () => createServerClient }); module.exports = __toCommonJS(createServerClient_exports); var import_supabase = require("@updatedev/js/supabase"); var import_cookies = require("@supabase/ssr/dist/main/cookies"); var import_version = require("@supabase/ssr/dist/main/version"); const createServerClient = (updateApiKey, supabaseUrl, supabaseAnonKey, options) => { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k; if (!updateApiKey) { throw new Error("An Update API Key is required"); } if (!supabaseUrl || !supabaseAnonKey) { throw new Error("A Supabase URL and Anon Key are required"); } const { storage, getAll, setAll, setItems, removedItems } = (0, import_cookies.createStorageFromOptions)( { cookies: options == null ? void 0 : options.cookies, cookieOptions: (_a = options == null ? void 0 : options.supabase) == null ? void 0 : _a.cookieOptions, cookieEncoding: (_c = (_b = options == null ? void 0 : options.supabase) == null ? void 0 : _b.cookieEncoding) != null ? _c : "base64url" }, true ); const supabaseOptions = { ...options == null ? void 0 : options.supabase, global: { ...(_d = options == null ? void 0 : options.supabase) == null ? void 0 : _d.global, headers: { ...(_f = (_e = options == null ? void 0 : options.supabase) == null ? void 0 : _e.global) == null ? void 0 : _f.headers, "X-Client-Info": `supabase-ssr/${import_version.VERSION} createServerClient` } }, auth: { ...((_h = (_g = options == null ? void 0 : options.supabase) == null ? void 0 : _g.auth) == null ? void 0 : _h.storageKey) ? { storageKey: options.supabase.auth.storageKey } : null, ...(_i = options == null ? void 0 : options.supabase) == null ? void 0 : _i.auth, flowType: "pkce", autoRefreshToken: false, detectSessionInUrl: false, persistSession: true, storage } }; const client = (0, import_supabase.createClient)( updateApiKey, supabaseUrl, supabaseAnonKey, { ...options, storage: { getAll: (_j = options == null ? void 0 : options.cookies) == null ? void 0 : _j.getAll, setAll: (_k = options == null ? void 0 : options.cookies) == null ? void 0 : _k.setAll }, supabase: supabaseOptions } ); client.auth.onAuthStateChange(async (event) => { var _a2, _b2, _c2, _d2; const hasStorageChanges = Object.keys(setItems).length > 0 || Object.keys(removedItems).length > 0; if (hasStorageChanges && (event === "SIGNED_IN" || event === "TOKEN_REFRESHED" || event === "USER_UPDATED" || event === "PASSWORD_RECOVERY" || event === "SIGNED_OUT" || event === "MFA_CHALLENGE_VERIFIED")) { await (0, import_cookies.applyServerStorage)( { getAll, setAll, setItems, removedItems }, { cookieOptions: (_b2 = (_a2 = options == null ? void 0 : options.supabase) == null ? void 0 : _a2.cookieOptions) != null ? _b2 : null, cookieEncoding: (_d2 = (_c2 = options == null ? void 0 : options.supabase) == null ? void 0 : _c2.cookieEncoding) != null ? _d2 : "base64url" } ); } }); return client; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createServerClient }); //# sourceMappingURL=createServerClient.js.map