7
7
} from '@supabase/postgrest-js'
8
8
import { RealtimeChannel , RealtimeClient , RealtimeClientOptions } from '@supabase/realtime-js'
9
9
import { SupabaseStorageClient } from '@supabase/storage-js'
10
- import { DEFAULT_HEADERS , STORAGE_KEY } from './lib/constants'
10
+ import { DEFAULT_HEADERS } from './lib/constants'
11
11
import { fetchWithAuth } from './lib/fetch'
12
12
import { isBrowser , stripTrailingSlash } from './lib/helpers'
13
13
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
@@ -49,6 +49,7 @@ export default class SupabaseClient<
49
49
protected realtime : RealtimeClient
50
50
protected rest : PostgrestClient < Database , SchemaName >
51
51
protected multiTab : boolean
52
+ protected storageKey : string
52
53
protected fetch ?: Fetch
53
54
protected changedAccessToken : string | undefined
54
55
protected shouldThrowOnError : boolean
@@ -79,7 +80,6 @@ export default class SupabaseClient<
79
80
if ( ! supabaseKey ) throw new Error ( 'supabaseKey is required.' )
80
81
81
82
const _supabaseUrl = stripTrailingSlash ( supabaseUrl )
82
- const settings = { ...DEFAULT_OPTIONS , ...options }
83
83
84
84
this . realtimeUrl = `${ _supabaseUrl } /realtime/v1` . replace ( 'http' , 'ws' )
85
85
this . authUrl = `${ _supabaseUrl } /auth/v1`
@@ -92,6 +92,11 @@ export default class SupabaseClient<
92
92
} else {
93
93
this . functionsUrl = `${ _supabaseUrl } /functions/v1`
94
94
}
95
+ // default storage key uses the supabase project ref as a namespace
96
+ const defaultStorageKey = `sb-${ new URL ( this . authUrl ) . hostname . split ( '.' ) [ 0 ] } -auth-token`
97
+ this . storageKey = options ?. auth ?. storageKey ?? defaultStorageKey
98
+
99
+ const settings = { ...DEFAULT_OPTIONS , ...options , storageKey : this . storageKey }
95
100
96
101
this . multiTab = settings . auth ?. multiTab ?? false
97
102
this . headers = { ...DEFAULT_HEADERS , ...options ?. headers }
@@ -267,6 +272,7 @@ export default class SupabaseClient<
267
272
localStorage,
268
273
cookieOptions,
269
274
multiTab,
275
+ storageKey,
270
276
} : SupabaseAuthClientOptions ,
271
277
headers ?: Record < string , string > ,
272
278
fetch ?: Fetch
@@ -278,6 +284,7 @@ export default class SupabaseClient<
278
284
return new SupabaseAuthClient ( {
279
285
url : this . authUrl ,
280
286
headers : { ...headers , ...authHeaders } ,
287
+ storageKey : storageKey ,
281
288
autoRefreshToken,
282
289
persistSession,
283
290
detectSessionInUrl,
@@ -302,7 +309,7 @@ export default class SupabaseClient<
302
309
303
310
try {
304
311
return window ?. addEventListener ( 'storage' , ( e : StorageEvent ) => {
305
- if ( e . key === STORAGE_KEY ) {
312
+ if ( e . key === this . storageKey ) {
306
313
const newSession = JSON . parse ( String ( e . newValue ) )
307
314
const accessToken : string | undefined =
308
315
newSession ?. currentSession ?. access_token ?? undefined
@@ -323,7 +330,7 @@ export default class SupabaseClient<
323
330
}
324
331
325
332
private _listenForAuthEvents ( ) {
326
- let { data } = this . auth . onAuthStateChange ( ( event , session ) => {
333
+ let data = this . auth . onAuthStateChange ( ( event , session ) => {
327
334
this . _handleTokenChanged ( event , session ?. access_token , 'CLIENT' )
328
335
} )
329
336
return data
0 commit comments