diff --git a/src/app/apolloCache/reactiveVars/authenticatedUserStore.ts b/src/app/apolloCache/reactiveVars/authenticatedUserStore.ts index 00d35d6c..8c1a1735 100644 --- a/src/app/apolloCache/reactiveVars/authenticatedUserStore.ts +++ b/src/app/apolloCache/reactiveVars/authenticatedUserStore.ts @@ -7,6 +7,36 @@ import { isConnectOnboardingNeededStore } from "./isConnectOnboardingNeededStore import type { EncodedAuthToken, AuthTokenPayload } from "@types"; class AuthenticatedUserStore extends ReactiveStore { + /** + * An override of `ReactiveStore.mergeUpdate` that shallow-merges all + * `AuthTokenPayload` properties, including nested objects. + */ + override mergeUpdate(partialNewValue: Partial) { + const currentValue = this.get(); + + const typeSafeCurrentValue = + currentValue && typeof currentValue === "object" + ? currentValue + : ({} as Partial); + + return this.set({ + ...typeSafeCurrentValue, + ...partialNewValue, + profile: { + ...(typeSafeCurrentValue?.profile ?? {}), + ...(partialNewValue?.profile ?? {}), + }, + subscription: { + ...(typeSafeCurrentValue?.subscription ?? {}), + ...(partialNewValue?.subscription ?? {}), + }, + stripeConnectAccount: { + ...(typeSafeCurrentValue?.stripeConnectAccount ?? {}), + ...(partialNewValue?.stripeConnectAccount ?? {}), + }, + } as AuthTokenPayload); + } + /** * Process an auth token to authenticate the user. */