Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import type {
Web3Credentials,
} from './lib/types'
import { stringToUint8Array, bytesToBase64URL } from './lib/base64url'
import { deepClone } from './lib/helpers'

polyfillGlobalThis() // Make "globalThis" available

Expand Down Expand Up @@ -2419,13 +2420,13 @@ export default class GoTrueClient {
const mainSessionData: Omit<Session, 'user'> & { user?: User } = { ...sessionToProcess }
delete mainSessionData.user // Remove user (real or proxy) before cloning for main storage

const clonedMainSessionData = structuredClone(mainSessionData)
const clonedMainSessionData = deepClone(mainSessionData)
await setItemAsync(this.storage, this.storageKey, clonedMainSessionData)
} else {
// No userStorage is configured.
// In this case, session.user should ideally not be a proxy.
// If it were, structuredClone would fail. This implies an issue elsewhere if user is a proxy here
const clonedSession = structuredClone(sessionToProcess) // sessionToProcess still has its original user property
const clonedSession = deepClone(sessionToProcess) // sessionToProcess still has its original user property
await setItemAsync(this.storage, this.storageKey, clonedSession)
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,11 @@ export function userNotAvailableProxy(): User {
},
})
}

/**
* Deep clones a JSON-serializable object using JSON.parse(JSON.stringify(obj)).
* Note: Only works for JSON-safe data.
*/
export function deepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj))
}