Skip to content

Commit

Permalink
Chore: Update Next.js and remove custom type for WritableCookies (#552)
Browse files Browse the repository at this point in the history
* update next.js and remove custom type
  • Loading branch information
dijonmusters authored May 21, 2023
1 parent 067402a commit 1086021
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-mugs-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-nextjs': patch
---

Upgrade Next.js and remove custom type for WritableRequestCookies
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@types/set-cookie-parser": "^2.4.2",
"@supabase/supabase-js": "2.19.0",
"config": "workspace:*",
"next": "^13.4.2",
"next": "^13.4.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^4.1.1",
Expand Down
8 changes: 0 additions & 8 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';
import type { CookieOptions } from '@supabase/auth-helpers-shared';

// Types
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';

// TODO! Remove this one 👇 when Next.js update their types for Route Handlers and Server Actions
export type WritableRequestCookies = ReadonlyRequestCookies & {
set: (name: string, value: string, options?: CookieOptions) => void;
};

export { createPagesBrowserClient } from './pagesBrowserClient';
export { createPagesServerClient } from './pagesServerClient';
export { createMiddlewareClient } from './middlewareClient';
Expand Down
17 changes: 5 additions & 12 deletions packages/nextjs/src/routeHandlerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
createSupabaseClient
} from '@supabase/auth-helpers-shared';

import type { WritableRequestCookies } from './index';
import { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';

class NextRouteHandlerAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: {
cookies: () => WritableRequestCookies;
cookies: () => ReadonlyRequestCookies;
},
cookieOptions?: CookieOptions
) {
Expand All @@ -29,6 +28,7 @@ class NextRouteHandlerAuthStorageAdapter extends CookieAuthStorageAdapter {
}
protected deleteCookie(name: string): void {
const nextCookies = this.context.cookies();
// @ts-expect-error Next.js need to fix their type for cookie options (3rd parameter on .set method)
nextCookies.set(name, '', {
maxAge: 0
});
Expand All @@ -42,8 +42,7 @@ export function createRouteHandlerClient<
: string & keyof Database
>(
context: {
// TODO! Remove `ReadonlyRequestCookies` when Next.js fixes ts defs for Route Handlers and Server Actions
cookies: () => ReadonlyRequestCookies | WritableRequestCookies;
cookies: () => ReadonlyRequestCookies;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
Expand All @@ -63,12 +62,6 @@ export function createRouteHandlerClient<
);
}

// TODO! Remove this type narrowing statement 👇 when Next.js fixes ts defs for Route Handlers and Server Actions
const narrowedContext = {
...context,
cookies: context.cookies as () => WritableRequestCookies
};

return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
Expand All @@ -80,7 +73,7 @@ export function createRouteHandlerClient<
},
auth: {
storageKey: cookieOptions?.name,
storage: new NextRouteHandlerAuthStorageAdapter(narrowedContext, cookieOptions)
storage: new NextRouteHandlerAuthStorageAdapter(context, cookieOptions)
}
});
}
68 changes: 34 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1086021

Please sign in to comment.