-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreateServerClient.ts
207 lines (198 loc) · 7.43 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
AuthChangeEvent,
createClient,
SupabaseClient,
} from "@supabase/supabase-js";
import type {
GenericSchema,
SupabaseClientOptions,
} from "@supabase/supabase-js/dist/module/lib/types";
import { VERSION } from "./version";
import { createStorageFromOptions, applyServerStorage } from "./cookies";
import type {
CookieOptionsWithName,
CookieMethodsServer,
CookieMethodsServerDeprecated,
} from "./types";
/**
* @deprecated Please specify `getAll` and `setAll` cookie methods instead of
* the `get`, `set` and `remove`. These will not be supported in the next major
* version.
*/
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> & {
cookieOptions?: CookieOptionsWithName;
cookies: CookieMethodsServerDeprecated;
cookieEncoding?: "raw" | "base64url" | "base64url+length";
},
): SupabaseClient<Database, SchemaName, Schema>;
/**
* Creates a Supabase Client for use on the server-side of a server-side
* rendering (SSR) framework.
*
* There are two categories of uses for this function: use in middlewares and
* use in pages, components or routes.
*
* **Use in middlewares.**
*
* Middlewares are functions that run before any rendering logic is executed on
* the server-side. They typically have access to request headers (cookies) and
* can modify both the request and response headers.
*
* In most SSR frameworks, to use Supabase correctly you *must set up a
* middleware* and use this function in it.
*
* When using this in a middleware, the `cookie` option must be configured to
* use both `getAll` and `setAll`. Alternatively you can use the `get`, `set`
* and `remove` functions. The latter are deprecated **and not recommended**
* for most use cases due to being difficult to use properly and they do not
* cover important edge cases. In future major versions of the library, the
* option to configure `get`, `set` and `remove` will be removed.
*
* **IMPORTANT:** Failing to implement `getAll` and `setAll` correctly (or the
* deprecated `get`, `set` and `remove`) including omitting them **will cause
* significant and difficult to debug authentication issues**. They will
* manifest as: random logouts, early session termination, JSON parsing errors,
* increased number of refresh token requests, or relying on garbage state.
*
* **Use in pages, components or routes.**
*
* To use Supabase features server-side rendered in pages, components or routes
* (a.k.a. actions / APIs) you must create a client with this function. Not all
* frameworks allow the ability to set cookies or response headers when pages
* or components are rendered. In those cases you _can omit `setAll` (or the
* deprecated `set`, `remove`) cookie option methods_. **It is strongly
* recommended that if the ability to set cookies and response headers is
* present, you should configure the `setAll` (or the deprecated `set` and
* `remove`) cookie access methods.**
*
* **IMPORTANT:** If the ability to set cookies or response headers is not
* available **middleware or an equivalent must be used.** Failing to do this
* will cause significant and difficult to debug authentication issues.
*
* When `setAll` (or the deprecated `set`, `remove`) cookie methods are not
* configured, the Supabase Client will emit a warning if it is used in a way
* that requires setting cookies. If you see this warning, it usually means
* that you are using the Supabase Client in a wrong way:
*
* - You should have, but did not configure a middleware client.
* - There is a bug in your middleware function.
* - You are using features of the Supabase Client that change the User, e.g.
* by calling `supabase.auth.updateUser()` on the server.
*
* Please consult the latest Supabase guides for advice on how to avoid common
* pitfalls depending on SSR framework.
*
* @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 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> & {
cookieOptions?: CookieOptionsWithName;
cookies: CookieMethodsServer;
cookieEncoding?: "raw" | "base64url" | "base64url+length";
},
): SupabaseClient<Database, SchemaName, Schema>;
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> & {
cookieOptions?: CookieOptionsWithName;
cookies: CookieMethodsServer | CookieMethodsServerDeprecated;
cookieEncoding?: "raw" | "base64url" | "base64url+length";
},
): SupabaseClient<Database, SchemaName, Schema> {
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 { storage, getAll, setAll, setItems, removedItems } =
createStorageFromOptions(
{
...options,
cookieEncoding: options?.cookieEncoding ?? "base64url+length",
},
true,
);
const client = createClient<Database, SchemaName, Schema>(
supabaseUrl,
supabaseKey,
{
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
"X-Client-Info": `supabase-ssr/${VERSION} createServerClient`,
},
},
auth: {
...(options?.cookieOptions?.name
? { storageKey: options.cookieOptions.name }
: null),
...options?.auth,
flowType: "pkce",
autoRefreshToken: false,
detectSessionInUrl: false,
persistSession: true,
storage,
},
},
);
client.auth.onAuthStateChange(async (event: AuthChangeEvent) => {
// The SIGNED_IN event is fired very often, but we don't need to
// apply the storage each time it fires, only if there are changes
// that need to be set -- which is if setItems / removeItems have
// data.
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: options?.cookieOptions ?? null,
cookieEncoding: options?.cookieEncoding ?? "base64url+length",
},
);
}
});
return client;
}