Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: namespace storage key #460

Merged
merged 4 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
15 changes: 11 additions & 4 deletions src/SupabaseClient.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -49,6 +49,7 @@ export default class SupabaseClient<
protected realtime: RealtimeClient
protected rest: PostgrestClient<Database, SchemaName>
protected multiTab: boolean
protected storageKey: string
protected fetch?: Fetch
protected changedAccessToken: string | undefined
protected shouldThrowOnError: boolean
Expand Down Expand Up @@ -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`
Expand All @@ -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 }
Expand Down Expand Up @@ -267,6 +272,7 @@ export default class SupabaseClient<
localStorage,
cookieOptions,
multiTab,
storageKey,
}: SupabaseAuthClientOptions,
headers?: Record<string, string>,
fetch?: Fetch
Expand All @@ -278,6 +284,7 @@ export default class SupabaseClient<
return new SupabaseAuthClient({
url: this.authUrl,
headers: { ...headers, ...authHeaders },
storageKey: storageKey,
autoRefreshToken,
persistSession,
detectSessionInUrl,
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion 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'
4 changes: 4 additions & 0 deletions src/lib/types.ts
Expand Up @@ -20,6 +20,10 @@ export type SupabaseClientOptions<SchemaName> = {
* 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.
*/
Expand Down