-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
52 lines (46 loc) · 2 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { createClient as _createClient, type SupabaseClient } from "@supabase/supabase-js";
import type {
SupabaseClientOptions,
} from "@supabase/supabase-js";
import type {
CreateFirebaseAuthClientProps,
} from "./auth.d.ts";
import type {
Database,
} from "./types.d.ts";
/**
*
* @param args `CreateFirebaseAuthClientProps` props
* @returns A function that creates a Supabase client with Firebase Auth of the currently logged in user.
* @see https://supabase.com/docs/guides/auth/third-party/firebase-auth
*/
export const createFirebaseAuthSupabaseClient = <D extends Database = Database>(
...args: CreateFirebaseAuthClientProps<D>
): <DBR extends D = D, S extends string & keyof D = "public">(schemaName?: S) => SupabaseClient<DBR, S> => {
const [{auth, accessToken, jwtToken}, {supabaseUrl, supabaseAnonKey}] = args;
const createClient = <
DB extends D = D,
S extends string & keyof D = "public",
>(
schemaName: S = "public" as S,
) => {
if (!auth && !accessToken && !jwtToken) throw new Error("No auth, accessToken, or jwtToken provided");
const options: SupabaseClientOptions<S> = { db: { schema: schemaName } };
if (auth || accessToken) {
options.accessToken = accessToken ?? (async () => ((await auth?.currentUser?.getIdToken(false)) ?? null) as string);
} else if (jwtToken) {
options.global = {
headers: { Authorization: `Bearer ${jwtToken}` },
};
} else console.warn("No JWT token provided, this will probably fail.");
return _createClient<DB, S>(supabaseUrl, supabaseAnonKey, options);
};
return createClient;
};
/**
* Get the JWT token from the Request headers.
* This is required to create a Supabase client with the correct permissions in Edge functions.
* @param headers Edge function's Request headers
* @returns The JWT token if it exists, otherwise null
*/
export const getJwtToken = (headers: Headers): string | null => headers.get("Authorization")?.replace("Bearer ", "") ?? null;