Files
supabase/packages/common/gotrue.ts
T
4278bd4ec1 fix: bump auth-js version to v2.67.3 (#31217)
* fix: bump auth-js version to v2.67.3

* chore: add supabase/auth and supabase/dashboard as codeowners

* Revert "chore: add supabase/auth and supabase/dashboard as codeowners"

This reverts commit 57fc5776ba.

* bump supabase-js version

* import user type from single gotrue

* fix auth-js version in studio

* remove auth-js dep in studio

* fix ts

* use catalog versions

* fix ts

* Bump realtime-js version to avoid having 2 versions.

* Add @supabase/realtime-js to the pnpm catalog.

* upgrade realtime-js

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-17 14:54:21 +08:00

116 lines
3.4 KiB
TypeScript

import { AuthClient, navigatorLock, User } from '@supabase/auth-js'
export const STORAGE_KEY = process.env.NEXT_PUBLIC_STORAGE_KEY || 'supabase.dashboard.auth.token'
export const AUTH_DEBUG_KEY =
process.env.NEXT_PUBLIC_AUTH_DEBUG_KEY || 'supabase.dashboard.auth.debug'
export const AUTH_DEBUG_PERSISTED_KEY =
process.env.NEXT_PUBLIC_AUTH_DEBUG_PERSISTED_KEY || 'supabase.dashboard.auth.debug.persist'
export const AUTH_NAVIGATOR_LOCK_DISABLED_KEY =
process.env.NEXT_PUBLIC_AUTH_NAVIGATOR_LOCK_KEY ||
'supabase.dashboard.auth.navigatorLock.disabled'
const debug =
process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
globalThis?.localStorage?.getItem(AUTH_DEBUG_KEY) === 'true'
const persistedDebug =
process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
globalThis?.localStorage?.getItem(AUTH_DEBUG_PERSISTED_KEY) === 'true'
const shouldEnableNavigatorLock =
process.env.NEXT_PUBLIC_IS_PLATFORM === 'true' &&
!(globalThis?.localStorage?.getItem(AUTH_NAVIGATOR_LOCK_DISABLED_KEY) === 'true')
const shouldDetectSessionInUrl = process.env.NEXT_PUBLIC_AUTH_DETECT_SESSION_IN_URL
? process.env.NEXT_PUBLIC_AUTH_DETECT_SESSION_IN_URL === 'true'
: true
const navigatorLockEnabled = !!(shouldEnableNavigatorLock && globalThis?.navigator?.locks)
if (shouldEnableNavigatorLock && !globalThis?.navigator?.locks) {
console.warn('This browser does not support the Navigator Locks API. Please update it.')
}
const tabId = Math.random().toString(16).substring(2)
let dbHandle = new Promise<IDBDatabase | null>((accept, _) => {
if (!persistedDebug) {
accept(null)
return
}
const request = indexedDB.open('auth-debug-log', 1)
request.onupgradeneeded = (event: any) => {
const db = event?.target?.result
if (!db) {
return
}
db.createObjectStore('events', { autoIncrement: true })
}
request.onsuccess = (event: any) => {
console.log('Opened persisted auth debug log IndexedDB database', tabId)
accept(event.target.result)
}
request.onerror = (event: any) => {
console.error('Failed to open persisted auth debug log IndexedDB database', event)
accept(null)
}
})
const logIndexedDB = (message: string, ...args: any[]) => {
console.log(message, ...args)
const copyArgs = structuredClone(args)
copyArgs.forEach((value) => {
if (typeof value === 'object' && value !== null) {
delete value.user
delete value.access_token
delete value.token_type
delete value.provider_token
}
})
;(async () => {
try {
const db = await dbHandle
if (!db) {
return
}
const tx = db.transaction(['events'], 'readwrite')
tx.onerror = (event: any) => {
console.error('Failed to write to persisted auth debug log IndexedDB database', event)
dbHandle = Promise.resolve(null)
}
const events = tx.objectStore('events')
events.add({
m: message.replace(/^GoTrueClient@/i, ''),
a: copyArgs,
l: window.location.pathname,
t: tabId,
})
} catch (e: any) {
console.error('Failed to log to persisted auth debug log IndexedDB database', e)
dbHandle = Promise.resolve(null)
}
})()
}
export const gotrueClient = new AuthClient({
url: process.env.NEXT_PUBLIC_GOTRUE_URL,
storageKey: STORAGE_KEY,
detectSessionInUrl: shouldDetectSessionInUrl,
debug: debug ? (persistedDebug ? logIndexedDB : true) : false,
lock: navigatorLockEnabled ? navigatorLock : undefined,
})
export type { User }