Skip to content

Commit 4257092

Browse files
committed
draft
1 parent 2a41ab7 commit 4257092

File tree

7 files changed

+196
-31
lines changed

7 files changed

+196
-31
lines changed

packages/supabase-auth-helpers-qwik/package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{
22
"name": "supabase-auth-helpers-qwik",
3-
"description": "A collection of framework specific Auth utilities for working with Supabase.",
4-
"version": "0.0.3",
3+
"description": "A collection of Qwik framework specific Auth utilities for working with Supabase.",
4+
"version": "0.0.4",
55
"author": "Qwik Team",
66
"bugs": "https://github.com/QwikDev/qwik/issues",
7-
"dependencies": {
8-
"@supabase/auth-helpers-shared": "^0.6.3"
9-
},
107
"devDependencies": {
118
"@builder.io/qwik": "workspace:^",
129
"@builder.io/qwik-city": "workspace:^",
13-
"@supabase/supabase-js": "2.44.4"
10+
"@supabase/supabase-js": "2.49.4",
11+
"@supabase/ssr": "0.6.1"
1412
},
1513
"exports": {
1614
".": {
@@ -31,7 +29,7 @@
3129
"license": "MIT",
3230
"main": "./lib/index.qwik.mjs",
3331
"peerDependencies": {
34-
"@supabase/supabase-js": "^2.44.4"
32+
"@supabase/supabase-js": "^2.49.4"
3533
},
3634
"publishConfig": {
3735
"access": "public"
@@ -40,7 +38,7 @@
4038
"repository": {
4139
"type": "git",
4240
"url": "https://github.com/QwikDev/qwik.git",
43-
"directory": "packages/qwik-auth"
41+
"directory": "packages/supabase-auth-helpers-qwik"
4442
},
4543
"scripts": {
4644
"build": "vite build --mode lib"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Methods
2-
export { createBrowserClient, createServerClient } from './utils/createSupabaseClient';
2+
// export { createBrowserClient, createServerClient } from './utils/createSupabaseClient';
3+
export { createBrowserClient } from './utils/createBrowserClient';
4+
export { createServerClient } from './utils/createServerClient';
35

46
// Types
57
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { RequestEventBase } from '@builder.io/qwik-city';
2+
import type { CookieOptions } from '@supabase/ssr';
3+
import { CookieAuthStorageAdapter, parseCookies, serializeCookie } from '@supabase/ssr';
4+
5+
export class QwikServerAuthStorageAdapter extends CookieAuthStorageAdapter {
6+
constructor(
7+
private readonly requestEv: RequestEventBase,
8+
cookieOptions?: CookieOptions
9+
) {
10+
super(cookieOptions);
11+
}
12+
13+
protected getCookie(name: string): string | null | undefined {
14+
return parseCookies(this.requestEv.request.headers.get('Cookie') ?? '')[name];
15+
}
16+
protected setCookie(name: string, value: string): void {
17+
const cookieStr = serializeCookie(name, value, {
18+
...this.cookieOptions,
19+
// Allow supabase-js on the client to read the cookie as well
20+
httpOnly: false,
21+
});
22+
this.requestEv.headers.append('set-cookie', cookieStr);
23+
}
24+
protected deleteCookie(name: string): void {
25+
const cookieStr = serializeCookie(name, '', {
26+
...this.cookieOptions,
27+
maxAge: 0,
28+
// Allow supabase-js on the client to read the cookie as well
29+
httpOnly: false,
30+
});
31+
this.requestEv.headers.append('set-cookie', cookieStr);
32+
}
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { SupabaseClient } from '@supabase/supabase-js';
2+
import {
3+
createBrowserClient as browserClient,
4+
type CookieMethodsBrowser,
5+
type CookieOptionsWithName,
6+
} from '@supabase/ssr';
7+
import type {
8+
GenericSchema,
9+
SupabaseClientOptions,
10+
} from '@supabase/supabase-js/dist/module/lib/types';
11+
12+
export function createBrowserClient<
13+
Database = any,
14+
SchemaName extends string & keyof Database = 'public' extends keyof Database
15+
? 'public'
16+
: string & keyof Database,
17+
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
18+
? Database[SchemaName]
19+
: any,
20+
>(
21+
supabaseUrl: string,
22+
supabaseKey: string,
23+
options?: SupabaseClientOptions<SchemaName> & {
24+
cookies?: CookieMethodsBrowser;
25+
cookieOptions?: CookieOptionsWithName;
26+
cookieEncoding?: 'raw' | 'base64url';
27+
isSingleton?: boolean;
28+
}
29+
): SupabaseClient<Database, SchemaName, Schema> {
30+
if (!supabaseUrl || !supabaseKey) {
31+
throw new Error(
32+
'supabaseUrl and supabaseKey are required to create a Supabase client! Find these under `Settings` > `API` in your Supabase dashboard.'
33+
);
34+
}
35+
36+
// return browserClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey);
37+
return browserClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
38+
...options,
39+
global: {
40+
...options?.global,
41+
headers: {
42+
...options?.global?.headers,
43+
'X-Client-Info': '[email protected]',
44+
},
45+
},
46+
auth: {
47+
storageKey: options?.cookieOptions?.name,
48+
storage: new BrowserCookieAuthStorageAdapter(cookieOptions),
49+
},
50+
});
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export function createServerClient<
2+
Database = any,
3+
SchemaName extends string & keyof Database = 'public' extends keyof Database
4+
? 'public'
5+
: string & keyof Database,
6+
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
7+
? Database[SchemaName]
8+
: any,
9+
>(
10+
supabaseUrl: string,
11+
supabaseKey: string,
12+
requestEv: RequestEventBase,
13+
opts?: {
14+
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
15+
cookieOptions?: CookieOptionsWithName;
16+
}
17+
): SupabaseClient<Database, SchemaName, Schema> {
18+
const options = opts?.options;
19+
const cookieOptions = opts?.cookieOptions;
20+
if (!supabaseUrl || !supabaseKey) {
21+
throw new Error(
22+
'supabaseUrl and supabaseKey are required to create a Supabase client! Find these under `Settings` > `API` in your Supabase dashboard.'
23+
);
24+
}
25+
26+
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
27+
...options,
28+
global: {
29+
...options?.global,
30+
headers: {
31+
...options?.global?.headers,
32+
'X-Client-Info': '[email protected]',
33+
},
34+
},
35+
auth: {
36+
storageKey: cookieOptions?.name,
37+
storage: new QwikServerAuthStorageAdapter(requestEv, cookieOptions),
38+
},
39+
});
40+
}

packages/supabase-auth-helpers-qwik/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineConfig((config) => {
1919
'@builder.io/qwik-city',
2020
'@builder.io/qwik/build',
2121
'@supabase/supabase-js',
22-
'@supabase/auth-helpers-shared',
22+
'@supabase/ssr',
2323
],
2424
},
2525
},

pnpm-lock.yaml

Lines changed: 62 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)