diff --git a/packages/shared/src/cookieAuthStorageAdapter.ts b/packages/shared/src/cookieAuthStorageAdapter.ts index ba73ef37..8d565374 100644 --- a/packages/shared/src/cookieAuthStorageAdapter.ts +++ b/packages/shared/src/cookieAuthStorageAdapter.ts @@ -15,12 +15,14 @@ export abstract class CookieAuthStorageAdapter implements StorageAdapter { }; } - protected abstract getCookie(name: string): string | undefined | null; + protected abstract getCookie( + name: string + ): string | undefined | null | Promise; protected abstract setCookie(name: string, value: string): void; protected abstract deleteCookie(name: string): void; - getItem(key: string): string | Promise | null { - const value = this.getCookie(key); + async getItem(key: string): Promise { + const value = await this.getCookie(key); if (!value) return null; diff --git a/packages/ssr/package.json b/packages/ssr/package.json index 595f1f03..d30b3ee6 100644 --- a/packages/ssr/package.json +++ b/packages/ssr/package.json @@ -34,7 +34,8 @@ "homepage": "https://github.com/supabase/auth-helpers#readme", "dependencies": { "cookie": "^0.5.0", - "ramda": "^0.29.0" + "ramda": "^0.29.0", + "@supabase/auth-helpers-shared": "workspace:*" }, "devDependencies": { "@supabase/supabase-js": "2.33.1", diff --git a/packages/ssr/src/createServerClient.ts b/packages/ssr/src/createServerClient.ts index 65c9c64b..66a28386 100644 --- a/packages/ssr/src/createServerClient.ts +++ b/packages/ssr/src/createServerClient.ts @@ -1,12 +1,36 @@ import { createClient } from '@supabase/supabase-js'; +import { CookieAuthStorageAdapter } from '@supabase/auth-helpers-shared'; import { mergeDeepRight } from 'ramda'; -import { DEFAULT_COOKIE_OPTIONS, isBrowser } from './utils'; +import { isBrowser } from './utils'; +import type { CookieOptionsWithName, ServerCookieMethods } from './types'; import type { GenericSchema, SupabaseClientOptions } from '@supabase/supabase-js/dist/module/lib/types'; -import type { CookieOptionsWithName, ServerCookieMethods } from './types'; + +class ServerCookieAuthStorageAdapter extends CookieAuthStorageAdapter { + constructor( + private readonly context: { + cookies: ServerCookieMethods; + }, + cookieOptions?: CookieOptionsWithName + ) { + super(cookieOptions); + } + + protected getCookie( + name: string + ): string | undefined | null | Promise { + return this.context.cookies.get(name) ?? null; + } + protected setCookie(name: string, value: string): void { + this.context.cookies.set(name, value, this.cookieOptions); + } + protected deleteCookie(name: string): void { + this.context.cookies.remove(name, this.cookieOptions); + } +} export function createServerClient< Database = any, @@ -50,17 +74,7 @@ export function createServerClient< autoRefreshToken: isBrowser(), detectSessionInUrl: isBrowser(), persistSession: true, - storage: { - getItem: async (key: string) => (await cookies.get(key)) ?? null, - setItem: async (key: string, value: string) => - await cookies.set(key, value, { - ...DEFAULT_COOKIE_OPTIONS, - ...cookieOptions, - maxAge: DEFAULT_COOKIE_OPTIONS.maxAge - }), - removeItem: async (key: string) => - await cookies.remove(key, { ...DEFAULT_COOKIE_OPTIONS, ...cookieOptions, maxAge: 0 }) - } + storage: new ServerCookieAuthStorageAdapter({ cookies: cookies }, cookieOptions) } }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 810471d2..d84580aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -408,6 +408,9 @@ importers: packages/ssr: dependencies: + '@supabase/auth-helpers-shared': + specifier: workspace:* + version: link:../shared cookie: specifier: ^0.5.0 version: 0.5.0