diff --git a/package-lock.json b/package-lock.json index b86dd3f4..d1bf1886 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@supabase/functions-js": "^1.3.3", - "@supabase/gotrue-js": "^1.23.0-next.3", + "@supabase/gotrue-js": "^1.23.0-next.6", "@supabase/postgrest-js": "^1.0.0-next.2", "@supabase/realtime-js": "^1.7.3", "@supabase/storage-js": "^1.7.0", @@ -919,11 +919,11 @@ } }, "node_modules/@supabase/gotrue-js": { - "version": "1.23.0-next.3", - "resolved": "https://registry.npmjs.org/@supabase/gotrue-js/-/gotrue-js-1.23.0-next.3.tgz", - "integrity": "sha512-f1M/XmLFukOmchptbF7SAR+wWlT/xwz74mxjL0T6g0QcguOc5uHsu0As89MnAF9Jmb4Qq60FG15gS+9AV1N70Q==", + "version": "1.23.0-next.6", + "resolved": "https://registry.npmjs.org/@supabase/gotrue-js/-/gotrue-js-1.23.0-next.6.tgz", + "integrity": "sha512-leBXrW2BSyE0brvif0cp8V8JmZBkp7vaVPrQri/Esk2ddxvqENGDCUGShuwBci3+HkrziuKGJy3etehZ481kgw==", "dependencies": { - "cross-fetch": "^3.0.6" + "cross-fetch": "^3.1.5" } }, "node_modules/@supabase/postgrest-js": { @@ -9097,11 +9097,11 @@ } }, "@supabase/gotrue-js": { - "version": "1.23.0-next.3", - "resolved": "https://registry.npmjs.org/@supabase/gotrue-js/-/gotrue-js-1.23.0-next.3.tgz", - "integrity": "sha512-f1M/XmLFukOmchptbF7SAR+wWlT/xwz74mxjL0T6g0QcguOc5uHsu0As89MnAF9Jmb4Qq60FG15gS+9AV1N70Q==", + "version": "1.23.0-next.6", + "resolved": "https://registry.npmjs.org/@supabase/gotrue-js/-/gotrue-js-1.23.0-next.6.tgz", + "integrity": "sha512-leBXrW2BSyE0brvif0cp8V8JmZBkp7vaVPrQri/Esk2ddxvqENGDCUGShuwBci3+HkrziuKGJy3etehZ481kgw==", "requires": { - "cross-fetch": "^3.0.6" + "cross-fetch": "^3.1.5" } }, "@supabase/postgrest-js": { diff --git a/package.json b/package.json index 6662df22..9e3fe1fa 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "dependencies": { "@supabase/functions-js": "^1.3.3", - "@supabase/gotrue-js": "^1.23.0-next.3", + "@supabase/gotrue-js": "^1.23.0-next.6", "@supabase/postgrest-js": "^1.0.0-next.2", "@supabase/realtime-js": "^1.7.3", "@supabase/storage-js": "^1.7.0", diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index d5ec76eb..4ecf68ac 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -7,7 +7,7 @@ import { } from '@supabase/postgrest-js' import { RealtimeChannel, RealtimeClient, RealtimeClientOptions } from '@supabase/realtime-js' import { SupabaseStorageClient } from '@supabase/storage-js' -import { DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants' +import { DEFAULT_HEADERS } from './lib/constants' import { fetchWithAuth } from './lib/fetch' import { isBrowser, stripTrailingSlash } from './lib/helpers' import { SupabaseAuthClient } from './lib/SupabaseAuthClient' @@ -49,6 +49,7 @@ export default class SupabaseClient< protected realtime: RealtimeClient protected rest: PostgrestClient protected multiTab: boolean + protected storageKey: string protected fetch?: Fetch protected changedAccessToken: string | undefined protected shouldThrowOnError: boolean @@ -79,7 +80,6 @@ export default class SupabaseClient< if (!supabaseKey) throw new Error('supabaseKey is required.') const _supabaseUrl = stripTrailingSlash(supabaseUrl) - const settings = { ...DEFAULT_OPTIONS, ...options } this.realtimeUrl = `${_supabaseUrl}/realtime/v1`.replace('http', 'ws') this.authUrl = `${_supabaseUrl}/auth/v1` @@ -92,6 +92,11 @@ export default class SupabaseClient< } else { this.functionsUrl = `${_supabaseUrl}/functions/v1` } + // default storage key uses the supabase project ref as a namespace + const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token` + this.storageKey = options?.auth?.storageKey ?? defaultStorageKey + + const settings = { ...DEFAULT_OPTIONS, ...options, storageKey: this.storageKey } this.multiTab = settings.auth?.multiTab ?? false this.headers = { ...DEFAULT_HEADERS, ...options?.headers } @@ -267,6 +272,7 @@ export default class SupabaseClient< localStorage, cookieOptions, multiTab, + storageKey, }: SupabaseAuthClientOptions, headers?: Record, fetch?: Fetch @@ -278,6 +284,7 @@ export default class SupabaseClient< return new SupabaseAuthClient({ url: this.authUrl, headers: { ...headers, ...authHeaders }, + storageKey: storageKey, autoRefreshToken, persistSession, detectSessionInUrl, @@ -302,7 +309,7 @@ export default class SupabaseClient< try { return window?.addEventListener('storage', (e: StorageEvent) => { - if (e.key === STORAGE_KEY) { + if (e.key === this.storageKey) { const newSession = JSON.parse(String(e.newValue)) const accessToken: string | undefined = newSession?.currentSession?.access_token ?? undefined @@ -323,7 +330,7 @@ export default class SupabaseClient< } private _listenForAuthEvents() { - let { data } = this.auth.onAuthStateChange((event, session) => { + let data = this.auth.onAuthStateChange((event, session) => { this._handleTokenChanged(event, session?.access_token, 'CLIENT') }) return data diff --git a/src/lib/constants.ts b/src/lib/constants.ts index c1628d3f..2a40f2b0 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -1,4 +1,3 @@ // constants.ts import { version } from './version' export const DEFAULT_HEADERS = { 'X-Client-Info': `supabase-js/${version}` } -export const STORAGE_KEY = 'supabase.auth.token' diff --git a/src/lib/types.ts b/src/lib/types.ts index 8f3203ae..0c5f54c6 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -20,6 +20,10 @@ export type SupabaseClientOptions = { * Automatically refreshes the token for logged in users. */ autoRefreshToken?: boolean + /** + * Optional key name used for storing tokens in local storage + */ + storageKey?: string /** * Whether to persist a logged in session to storage. */