-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
createServerClient.ts
122 lines (114 loc) · 3.6 KB
/
createServerClient.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
import { createClient } from '@supabase/supabase-js';
import { mergeDeepRight } from 'ramda';
import {
DEFAULT_COOKIE_OPTIONS,
combineChunks,
createChunks,
deleteChunks,
isBrowser
} from './utils';
import type {
GenericSchema,
SupabaseClientOptions
} from '@supabase/supabase-js/dist/module/lib/types';
import type { CookieOptionsWithName, CookieMethods } from './types';
export function createServerClient<
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: CookieMethods;
cookieOptions?: CookieOptionsWithName;
}
) {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
`Your project's URL and 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 { cookies, cookieOptions, ...userDefinedClientOptions } = options;
// use the cookie name as the storageKey value if it's set
if (cookieOptions?.name) {
userDefinedClientOptions.auth = {
...userDefinedClientOptions.auth,
storageKey: cookieOptions.name
};
}
const cookieClientOptions = {
global: {
headers: {
'X-Client-Info': `${PACKAGE_NAME}/${PACKAGE_VERSION}`
}
},
auth: {
flowType: 'pkce',
autoRefreshToken: isBrowser(),
detectSessionInUrl: isBrowser(),
persistSession: true,
storage: {
// to signal to the libraries that these cookies are coming from a server environment and their value should not be trusted
isServer: true,
getItem: async (key: string) => {
const chunkedCookie = await combineChunks(key, async (chunkName: string) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
});
return chunkedCookie;
},
setItem: async (key: string, value: string) => {
const chunks = createChunks(key, value);
await Promise.all(
chunks.map(async (chunk) => {
if (typeof cookies.set === 'function') {
await cookies.set(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
}
})
);
},
removeItem: async (key: string) => {
if (typeof cookies.remove === 'function' && typeof cookies.get !== 'function') {
console.log(
'Removing chunked cookie without a `get` method is not supported.\n\n\tWhen you call the `createServerClient` function from the `@supabase/ssr` package, make sure you declare both a `get` and `remove` method on the `cookies` object.\n\nhttps://supabase.com/docs/guides/auth/server-side/creating-a-client'
);
return;
}
deleteChunks(
key,
async (chunkName) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
},
async (chunkName) => {
if (typeof cookies.remove === 'function') {
return await cookies.remove(chunkName, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
}
}
);
}
}
}
};
// Overwrites default client config with any user defined options
const clientOptions = mergeDeepRight(
cookieClientOptions,
userDefinedClientOptions
) as SupabaseClientOptions<SchemaName>;
return createClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, clientOptions);
}