Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix[mobile]: persist mobile auth (#3037)
Peter Chinman300f5610ebaa
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsxindex 095c46b08e..b1e957e106 100644--- a/js/app/packages/app/component/Root.tsx+++ b/js/app/packages/app/component/Root.tsx@@ -16,7 +16,12 @@ import { IosPushNotificationModal } from '@core/mobile/IosPushNotificationModal'import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';import { createBlockOrchestrator } from '@core/orchestrator';import { formatTabTitle, tabTitleSignal } from '@core/signal/tabTitle';-import { getLoginCookieOptions, updateCookie } from '@core/util/cookies';+import {+ getLoginCookieOptions,+ hasLoginCookie,+ syncLoginStorage,+ updateCookie,+} from '@core/util/cookies';import { licenseChannel } from '@core/util/licenseUpdateBroadcastChannel';import { isTauri } from '@core/util/platform';import { transformShortIdInUrlPathname } from '@core/util/url';@@ -54,6 +59,7 @@ import { useHotKeyRoot } from 'core/hotkey/hotkeys';import { detect } from 'detect-browser';import {createEffect,+ createSignal,type JSX,Match,on,@@ -93,6 +99,7 @@ import {import { PosthogProvider, usePosthog } from '@app/lib/analytics/posthog';import { CallProvider } from '@channel/Call/CallContext';import { CallStartedNotifier } from '@channel/Call/CallStartedNotifier';+import { Button } from '@ui';/** Syncs login cookie with auth state. Only updates on successful query (not errors/loading). */function useSyncLoginCookie() {@@ -101,10 +108,10 @@ function useSyncLoginCookie() {createEffect(() => {if (!userInfoQuery.isSuccess) return;- const { value, ...options } = getLoginCookieOptions(- userInfoQuery.data.authenticated ?? false- );+ const authenticated = userInfoQuery.data.authenticated ?? false;+ const { value, ...options } = getLoginCookieOptions(authenticated);updateCookie('login', value, options);+ syncLoginStorage(authenticated);});}@@ -150,6 +157,30 @@ const rootPreload: RoutePreloadFunc = async (args) => {}};+function OfflineFallback(props: { onRetry: () => Promise<unknown> }) {+ const [retrying, setRetrying] = createSignal(false);++ const handleRetry = async () => {+ setRetrying(true);+ await props.onRetry();+ setRetrying(false);+ };++ return (+ <div class="flex flex-col items-center justify-center gap-4 h-full w-full text-ink-muted">+ <p class="text-sm">Unable to connect. Please check your network.</p>+ <Button+ class="mt-2"+ disabled={retrying()}+ onClick={handleRetry}+ variant="primary"+ >+ {retrying() ? 'Retrying…' : 'Retry'}+ </Button>+ </div>+ );+}+function BasePathComponent() {const analytics = useAnalytics();@@ -192,12 +223,17 @@ function BasePathComponent() {return (<Switch><Match when={userInfoQuery.isLoading}>{null}</Match>+ <Match+ when={+ userInfoQuery.isError && hasLoginCookie() && isNativeMobilePlatform()+ }+ >+ <OfflineFallback onRetry={() => userInfoQuery.refetch()} />+ </Match><Matchwhen={!userInfoQuery.isLoading && !userInfoQuery.data?.authenticated}>- <Navigate- href={`${isNativeMobilePlatform() ? '/welcome' : '/welcome'}${window.location.search}`}- />+ <Navigate href={`/welcome${window.location.search}`} /></Match><Match when={userInfoQuery.data?.authenticated}><Navigate href={redirectPath} />diff --git a/js/app/packages/core/auth/logout.ts b/js/app/packages/core/auth/logout.tsindex f92706533f..f2812c98fe 100644--- a/js/app/packages/core/auth/logout.ts+++ b/js/app/packages/core/auth/logout.ts@@ -2,6 +2,7 @@ import { authServiceClient } from '@service-auth/client';import { authKeys } from '@queries/auth/user-info';import { queryClient } from '@queries/client';import { SERVER_HOSTS } from '@core/constant/servers';+import { syncLoginStorage } from '@core/util/cookies';import { createCallback } from '@solid-primitives/rootless';import { useAnalytics } from '@app/component/analytics-context';import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';@@ -14,6 +15,7 @@ export function useLogout() {return createCallback(async () => {document.cookie ='login=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; max-age=0; path=/; SameSite=Lax';+ syncLoginStorage(false);queryClient.setQueryData(authKeys.userInfo.queryKey, {id: '',diff --git a/js/app/packages/core/util/cookies.ts b/js/app/packages/core/util/cookies.tsindex a3bc5bca03..6c5c84fa3e 100644--- a/js/app/packages/core/util/cookies.ts+++ b/js/app/packages/core/util/cookies.ts@@ -15,8 +15,16 @@ export type LoginCookieOptions = {sameSite: 'Lax';};-/** Check if the user appears to be authenticated based on the login cookie. */+const LOGIN_STORAGE_KEY = 'macro:login';++/** Check if the user appears to be authenticated based on the login cookie or localStorage fallback. */export function hasLoginCookie(): boolean {+ if (+ typeof localStorage !== 'undefined' &&+ localStorage.getItem(LOGIN_STORAGE_KEY) === 'true'+ ) {+ return true;+ }if (typeof document === 'undefined') return false;const cookies = document.cookie.split(';');for (const cookie of cookies) {@@ -28,6 +36,16 @@ export function hasLoginCookie(): boolean {return false;}+/** Sync the login state to localStorage for environments where cookies don't persist, e.g. Tauri. */+export function syncLoginStorage(isAuthenticated: boolean) {+ if (typeof localStorage === 'undefined') return;+ if (isAuthenticated) {+ localStorage.setItem(LOGIN_STORAGE_KEY, 'true');+ } else {+ localStorage.removeItem(LOGIN_STORAGE_KEY);+ }+}+/** Compute login cookie options from auth state. */export function getLoginCookieOptions(isAuthenticated: booleandiff --git a/js/app/packages/queries/persistence-scopes.ts b/js/app/packages/queries/persistence-scopes.tsindex af60c494ac..dbca0bd07d 100644--- a/js/app/packages/queries/persistence-scopes.ts+++ b/js/app/packages/queries/persistence-scopes.ts@@ -1,4 +1,7 @@import { partialMatchKey, type QueryKey } from '@tanstack/query-core';+import { hasLoginCookie } from '@core/util/cookies';+import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';+import { authKeys } from './auth/keys';import { channelKeys } from './channel/keys';import { createPersistenceKey, type PersistScope } from './persistence';import { createPerQueryIDBStore } from './persistence/per-query-idb';@@ -36,5 +39,19 @@ export function createQueryPersistenceScopes(shouldPersist: (queryKey) =>partialMatchKey(queryKey, ['email', 'threadMessages']),},+ ...(isNativeMobilePlatform()+ ? [+ {+ store: createPerQueryIDBStore({+ dbName: createPersistenceKey('user-info', 1),+ }),+ maxAge: { value: 7, unit: 'd' },+ buster,+ shouldPersist: (queryKey: QueryKey) =>+ partialMatchKey(queryKey, authKeys.userInfo.queryKey),+ shouldRestore: hasLoginCookie,+ } satisfies PersistScope,+ ]+ : []),];}diff --git a/js/app/packages/queries/persistence.ts b/js/app/packages/queries/persistence.tsindex 64897b1ed2..71238248d3 100644--- a/js/app/packages/queries/persistence.ts+++ b/js/app/packages/queries/persistence.ts@@ -27,6 +27,7 @@ export type PersistScope = Readonly<{maxAge: ParsedDuration;buster: string;shouldPersist: (queryKey: QueryKey) => boolean;+ shouldRestore?: (queryKey: QueryKey) => boolean;}>;type QueryClientLike = {@@ -68,6 +69,8 @@ async function handleRestore(scope: PersistScope,query: Query): Promise<void> {+ if (scope.shouldRestore && !scope.shouldRestore(query.queryKey)) return;+const state = queryClient.getQueryState(query.queryKey);if (state && state.status === 'success') return;