Skip to content

Commit

Permalink
feat: namespace storage key (#460)
Browse files Browse the repository at this point in the history
* fix: use project ref as namespace

* fix: allow supabase client to pass custom storage key

* fix return value for onAuthStateChange
  • Loading branch information
kangmingtay committed Aug 1, 2022
1 parent 8503715 commit 1cba0b5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
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

0 comments on commit 1cba0b5

Please sign in to comment.