Skip to content

Commit 1d1d581

Browse files
committed
fix: Only warn if multiple clients share a storage-key
This allows use of multiple clients with different storage-keys, without the `Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key.` warning appearing.
1 parent 5cd695c commit 1d1d581

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/GoTrueClient.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ async function lockNoOp<R>(name: string, acquireTimeout: number, fn: () => Promi
156156
const GLOBAL_JWKS: { [storageKey: string]: { cachedAt: number; jwks: { keys: JWK[] } } } = {}
157157

158158
export default class GoTrueClient {
159-
private static nextInstanceID = 0
159+
private static nextInstanceID: Record<string, number> = {}
160160

161161
private instanceID: number
162162

@@ -238,24 +238,24 @@ export default class GoTrueClient {
238238
* Create a new client for use in the browser.
239239
*/
240240
constructor(options: GoTrueClientOptions) {
241-
this.instanceID = GoTrueClient.nextInstanceID
242-
GoTrueClient.nextInstanceID += 1
241+
const settings = { ...DEFAULT_OPTIONS, ...options }
242+
this.storageKey = settings.storageKey
243+
244+
this.instanceID = GoTrueClient.nextInstanceID[this.storageKey] ?? 0
245+
GoTrueClient.nextInstanceID[this.storageKey] = this.instanceID + 1
243246

244247
if (this.instanceID > 0 && isBrowser()) {
245248
console.warn(
246249
'Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key.'
247250
)
248251
}
249252

250-
const settings = { ...DEFAULT_OPTIONS, ...options }
251-
252253
this.logDebugMessages = !!settings.debug
253254
if (typeof settings.debug === 'function') {
254255
this.logger = settings.debug
255256
}
256257

257258
this.persistSession = settings.persistSession
258-
this.storageKey = settings.storageKey
259259
this.autoRefreshToken = settings.autoRefreshToken
260260
this.admin = new GoTrueAdminApi({
261261
url: settings.url,
@@ -337,7 +337,8 @@ export default class GoTrueClient {
337337
private _debug(...args: any[]): GoTrueClient {
338338
if (this.logDebugMessages) {
339339
this.logger(
340-
`GoTrueClient@${this.instanceID} (${version}) ${new Date().toISOString()}`,
340+
'GoTrueClient@' +
341+
`${this.storageKey}:${this.instanceID} (${version}) ${new Date().toISOString()}`,
341342
...args
342343
)
343344
}

0 commit comments

Comments
 (0)