-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreateBrowserClient.ts
127 lines (115 loc) · 4.01 KB
/
createBrowserClient.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { createClient, SupabaseClient } from "@supabase/supabase-js";
import type {
GenericSchema,
SupabaseClientOptions,
} from "@supabase/supabase-js/dist/module/lib/types";
import { VERSION } from "./version";
import { isBrowser } from "./utils";
import type {
CookieMethodsBrowser,
CookieOptionsWithName,
} from "./types";
import { createStorageFromOptions } from "./cookies";
let cachedBrowserClient: SupabaseClient<any, any, any> | undefined;
/**
* Creates a Supabase Client for use in a browser environment.
*
* In most cases you should not configure the `options.cookies` object, as this
* is automatically handled for you. If you do customize this, prefer using the
* `getAll` and `setAll` functions over `get`, `set` and `remove`. The latter
* are deprecated due to being difficult to correctly implement and not
* supporting some edge-cases. Both `getAll` and `setAll` (or both `get`, `set`
* and `remove`) must be provided. Failing to provide the methods for setting
* will throw an exception, and in previous versions of the library will result
* in difficult to debug authentication issues such as random logouts, early
* session termination or problems with inconsistent state.
*
* @param supabaseUrl The URL of the Supabase project.
* @param supabaseKey The `anon` API key of the Supabase project.
* @param options Various configuration options.
*/
export function createBrowserClient<
Database = any,
SchemaName extends string & keyof Database = "public" extends keyof Database
? "public"
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any,
>(
supabaseUrl: string,
supabaseKey: string,
options?: SupabaseClientOptions<SchemaName> & {
cookies: CookieMethodsBrowser;
cookieOptions?: CookieOptionsWithName;
cookieEncoding?: "raw" | "base64url";
isSingleton?: boolean;
},
): SupabaseClient<Database, SchemaName, Schema>;
export function createBrowserClient<
Database = any,
SchemaName extends string & keyof Database = "public" extends keyof Database
? "public"
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any,
>(
supabaseUrl: string,
supabaseKey: string,
options?: SupabaseClientOptions<SchemaName> & {
cookies?: CookieMethodsBrowser;
cookieOptions?: CookieOptionsWithName;
cookieEncoding?: "raw" | "base64url";
isSingleton?: boolean;
},
): SupabaseClient<Database, SchemaName, Schema> {
// singleton client is created only if isSingleton is set to true, or if isSingleton is not defined and we detect a browser
const shouldUseSingleton =
options?.isSingleton === true ||
((!options || !("isSingleton" in options)) && isBrowser());
if (shouldUseSingleton && cachedBrowserClient) {
return cachedBrowserClient;
}
if (!supabaseUrl || !supabaseKey) {
throw new Error(
`@supabase/ssr: Your project's URL and API key are required to create a Supabase client!\n\nCheck your Supabase project's API settings to find these values\n\nhttps://supabase.com/dashboard/project/_/settings/api`,
);
}
const { storage } = createStorageFromOptions(
{
...options,
cookieEncoding: options?.cookieEncoding ?? "base64url",
},
false,
);
const client = createClient<Database, SchemaName, Schema>(
supabaseUrl,
supabaseKey,
{
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
"X-Client-Info": `supabase-ssr/${VERSION} createBrowserClient`,
},
},
auth: {
...options?.auth,
...(options?.cookieOptions?.name
? { storageKey: options.cookieOptions.name }
: null),
flowType: "pkce",
autoRefreshToken: isBrowser(),
detectSessionInUrl: isBrowser(),
persistSession: true,
storage,
},
},
);
if (shouldUseSingleton) {
cachedBrowserClient = client;
}
return client;
}