• Authenticated Supabase client

    Handle

    import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from '$env/static/public';
    import { createSupabaseServerClient } from '@supabase/auth-helpers-sveltekit';
    import type { Handle } from '@sveltejs/kit';

    export const handle: Handle = async ({ event, resolve }) => {
    event.locals.supabase = createSupabaseServerClient({
    supabaseUrl: PUBLIC_SUPABASE_URL,
    supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
    event
    });

    event.locals.getSession = async () => {
    const {
    data: { session }
    } = await event.locals.supabase.auth.getSession();
    return session;
    };

    return resolve(event, {
    filterSerializedResponseHeaders(name) {
    return name === 'content-range';
    }
    });
    };

    Types

    import { SupabaseClient, Session } from '@supabase/supabase-js';

    declare global {
    namespace App {
    interface Locals {
    supabase: SupabaseClient;
    getSession(): Promise<Session | null>;
    }
    // interface PageData {}
    // interface Error {}
    // interface Platform {}
    }
    }

    export {};

    Type Parameters

    • Database = any

    • SchemaName extends string = "public" extends keyof Database
          ? "public"
          : string & keyof Database

    • Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
          ? any[any]
          : any

    Parameters

    • __namedParameters: {
          cookieOptions?: CookieOptionsWithName;
          event: Pick<RequestEvent<Partial<Record<string, string>>, null | string>, "cookies">;
          expiryMargin?: number;
          options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
          supabaseKey: string;
          supabaseUrl: string;
      }
      • Optional cookieOptions?: CookieOptionsWithName
      • event: Pick<RequestEvent<Partial<Record<string, string>>, null | string>, "cookies">
      • Optional expiryMargin?: number
      • Optional options?: SupabaseClientOptionsWithoutAuth<SchemaName>
      • supabaseKey: string
      • supabaseUrl: string

    Returns SupabaseClient<Database, SchemaName, Schema>