Skip to content

Commit 1cba0b5

Browse files
authored
feat: namespace storage key (#460)
* fix: use project ref as namespace * fix: allow supabase client to pass custom storage key * fix return value for onAuthStateChange
1 parent 8503715 commit 1cba0b5

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"dependencies": {
4040
"@supabase/functions-js": "^1.3.3",
41-
"@supabase/gotrue-js": "^1.23.0-next.3",
41+
"@supabase/gotrue-js": "^1.23.0-next.6",
4242
"@supabase/postgrest-js": "^1.0.0-next.2",
4343
"@supabase/realtime-js": "^1.7.3",
4444
"@supabase/storage-js": "^1.7.0",

src/SupabaseClient.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@supabase/postgrest-js'
88
import { RealtimeChannel, RealtimeClient, RealtimeClientOptions } from '@supabase/realtime-js'
99
import { SupabaseStorageClient } from '@supabase/storage-js'
10-
import { DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
10+
import { DEFAULT_HEADERS } from './lib/constants'
1111
import { fetchWithAuth } from './lib/fetch'
1212
import { isBrowser, stripTrailingSlash } from './lib/helpers'
1313
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
@@ -49,6 +49,7 @@ export default class SupabaseClient<
4949
protected realtime: RealtimeClient
5050
protected rest: PostgrestClient<Database, SchemaName>
5151
protected multiTab: boolean
52+
protected storageKey: string
5253
protected fetch?: Fetch
5354
protected changedAccessToken: string | undefined
5455
protected shouldThrowOnError: boolean
@@ -79,7 +80,6 @@ export default class SupabaseClient<
7980
if (!supabaseKey) throw new Error('supabaseKey is required.')
8081

8182
const _supabaseUrl = stripTrailingSlash(supabaseUrl)
82-
const settings = { ...DEFAULT_OPTIONS, ...options }
8383

8484
this.realtimeUrl = `${_supabaseUrl}/realtime/v1`.replace('http', 'ws')
8585
this.authUrl = `${_supabaseUrl}/auth/v1`
@@ -92,6 +92,11 @@ export default class SupabaseClient<
9292
} else {
9393
this.functionsUrl = `${_supabaseUrl}/functions/v1`
9494
}
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 }
95100

96101
this.multiTab = settings.auth?.multiTab ?? false
97102
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
@@ -267,6 +272,7 @@ export default class SupabaseClient<
267272
localStorage,
268273
cookieOptions,
269274
multiTab,
275+
storageKey,
270276
}: SupabaseAuthClientOptions,
271277
headers?: Record<string, string>,
272278
fetch?: Fetch
@@ -278,6 +284,7 @@ export default class SupabaseClient<
278284
return new SupabaseAuthClient({
279285
url: this.authUrl,
280286
headers: { ...headers, ...authHeaders },
287+
storageKey: storageKey,
281288
autoRefreshToken,
282289
persistSession,
283290
detectSessionInUrl,
@@ -302,7 +309,7 @@ export default class SupabaseClient<
302309

303310
try {
304311
return window?.addEventListener('storage', (e: StorageEvent) => {
305-
if (e.key === STORAGE_KEY) {
312+
if (e.key === this.storageKey) {
306313
const newSession = JSON.parse(String(e.newValue))
307314
const accessToken: string | undefined =
308315
newSession?.currentSession?.access_token ?? undefined
@@ -323,7 +330,7 @@ export default class SupabaseClient<
323330
}
324331

325332
private _listenForAuthEvents() {
326-
let { data } = this.auth.onAuthStateChange((event, session) => {
333+
let data = this.auth.onAuthStateChange((event, session) => {
327334
this._handleTokenChanged(event, session?.access_token, 'CLIENT')
328335
})
329336
return data

src/lib/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
// constants.ts
22
import { version } from './version'
33
export const DEFAULT_HEADERS = { 'X-Client-Info': `supabase-js/${version}` }
4-
export const STORAGE_KEY = 'supabase.auth.token'

src/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export type SupabaseClientOptions<SchemaName> = {
2020
* Automatically refreshes the token for logged in users.
2121
*/
2222
autoRefreshToken?: boolean
23+
/**
24+
* Optional key name used for storing tokens in local storage
25+
*/
26+
storageKey?: string
2327
/**
2428
* Whether to persist a logged in session to storage.
2529
*/

0 commit comments

Comments
 (0)