Skip to content

Commit

Permalink
simplifying defaults for storage adapter (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-plugge committed Apr 13, 2023
1 parent d156863 commit 66b13b9
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 202 deletions.
8 changes: 8 additions & 0 deletions .changeset/silly-beds-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@supabase/auth-helpers-nextjs': patch
'@supabase/auth-helpers-remix': patch
'@supabase/auth-helpers-shared': patch
'@supabase/auth-helpers-sveltekit': patch
---

simplifying defaults for storage adapter
26 changes: 7 additions & 19 deletions packages/nextjs/src/browserClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
BrowserCookieAuthStorageAdapter,
CookieOptions,
DEFAULT_COOKIE_OPTIONS,
SupabaseClientOptionsWithoutAuth
CookieOptionsWithName,
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { createClient } from '@supabase/supabase-js';

export function createBrowserSupabaseClient<
Database = any,
Expand All @@ -20,15 +19,15 @@ export function createBrowserSupabaseClient<
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptions;
cookieOptions?: CookieOptionsWithName;
} = {}) {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}

return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand All @@ -38,19 +37,8 @@ export function createBrowserSupabaseClient<
}
},
auth: {
flowType: 'pkce',

// fix this in supabase-js
...(cookieOptions?.name
? {
storageKey: cookieOptions.name
}
: {}),

storage: new BrowserCookieAuthStorageAdapter({
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions
})
storageKey: cookieOptions?.name,
storage: new BrowserCookieAuthStorageAdapter(cookieOptions)
}
});
}
29 changes: 8 additions & 21 deletions packages/nextjs/src/middlewareClient.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
DEFAULT_COOKIE_OPTIONS,
CookieOptionsWithName,
createSupabaseClient,
parseCookies,
serializeCookie,
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { createClient, SupabaseClientOptions } from '@supabase/supabase-js';
import { NextRequest, NextResponse } from 'next/server';

class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: { req: NextRequest; res: NextResponse },
private readonly cookieOptions?: CookieOptions
cookieOptions?: CookieOptions
) {
super();
super(cookieOptions);
}

protected getCookie(name: string): string | null | undefined {
Expand Down Expand Up @@ -59,7 +59,7 @@ export function createMiddlewareSupabaseClient<
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptions;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
if (!supabaseUrl || !supabaseKey) {
Expand All @@ -68,7 +68,7 @@ export function createMiddlewareSupabaseClient<
);
}

return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand All @@ -78,21 +78,8 @@ export function createMiddlewareSupabaseClient<
}
},
auth: {
flowType: 'pkce',
autoRefreshToken: false,
detectSessionInUrl: false,

// fix this in supabase-js
...(cookieOptions?.name
? {
storageKey: cookieOptions.name
}
: {}),

storage: new NextMiddlewareAuthStorageAdapter(context, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions
})
storageKey: cookieOptions?.name,
storage: new NextMiddlewareAuthStorageAdapter(context, cookieOptions)
}
});
}
29 changes: 8 additions & 21 deletions packages/nextjs/src/serverClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
DEFAULT_COOKIE_OPTIONS,
CookieOptionsWithName,
createSupabaseClient,
parseCookies,
serializeCookie,
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { createClient } from '@supabase/supabase-js';
import {
GetServerSidePropsContext,
NextApiRequest,
Expand All @@ -19,9 +19,9 @@ class NextServerAuthStorageAdapter extends CookieAuthStorageAdapter {
private readonly context:
| GetServerSidePropsContext
| { req: NextApiRequest; res: NextApiResponse },
private readonly cookieOptions?: CookieOptions
cookieOptions?: CookieOptions
) {
super();
super(cookieOptions);
}

protected getCookie(name: string): string | null | undefined {
Expand Down Expand Up @@ -79,7 +79,7 @@ export function createServerSupabaseClient<
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptions;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
if (!supabaseUrl || !supabaseKey) {
Expand All @@ -88,7 +88,7 @@ export function createServerSupabaseClient<
);
}

return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand All @@ -98,21 +98,8 @@ export function createServerSupabaseClient<
}
},
auth: {
flowType: 'pkce',
autoRefreshToken: false,
detectSessionInUrl: false,

// fix this in supabase-js
...(cookieOptions?.name
? {
storageKey: cookieOptions.name
}
: {}),

storage: new NextServerAuthStorageAdapter(context, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions
})
storageKey: cookieOptions?.name,
storage: new NextServerAuthStorageAdapter(context, cookieOptions)
}
});
}
31 changes: 9 additions & 22 deletions packages/nextjs/src/serverComponentClient.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
DEFAULT_COOKIE_OPTIONS,
SupabaseClientOptionsWithoutAuth
CookieOptionsWithName,
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { createClient } from '@supabase/supabase-js';

class NextServerComponentAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: {
cookies: () => any; // TODO update this to be ReadonlyHeaders when we upgrade to Next.js 13
},
private readonly cookieOptions?: CookieOptions
cookieOptions?: CookieOptions
) {
super();
super(cookieOptions);
}

protected getCookie(name: string): string | null | undefined {
Expand Down Expand Up @@ -53,7 +53,7 @@ export function createServerComponentSupabaseClient<
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptions;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
if (!supabaseUrl || !supabaseKey) {
Expand All @@ -62,7 +62,7 @@ export function createServerComponentSupabaseClient<
);
}

return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
Expand All @@ -72,21 +72,8 @@ export function createServerComponentSupabaseClient<
}
},
auth: {
flowType: 'pkce',
autoRefreshToken: false,
detectSessionInUrl: false,

// fix this in supabase-js
...(cookieOptions?.name
? {
storageKey: cookieOptions.name
}
: {}),

storage: new NextServerComponentAuthStorageAdapter(context, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions
})
storageKey: cookieOptions?.name,
storage: new NextServerComponentAuthStorageAdapter(context, cookieOptions)
}
});
}
6 changes: 0 additions & 6 deletions packages/nextjs/src/types.ts

This file was deleted.

0 comments on commit 66b13b9

Please sign in to comment.