UNPKG

@updatedev/ssr

Version:
75 lines 3.1 kB
import { createClient } from "@updatedev/js/supabase"; import { applyServerStorage, createStorageFromOptions } from "@supabase/ssr/dist/main/cookies"; import { VERSION } from "@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 } = 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/${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 = 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 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; }; export { createServerClient }; //# sourceMappingURL=createServerClient.js.map