Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(auth): only clear login cookie when auth state is actually known (#1086)

teo2dfd1f906083
diff --git a/js/app/packages/app/component/Layout.tsx b/js/app/packages/app/component/Layout.tsx
index dd2bc07f03..d73aca6aa4 100644
--- a/js/app/packages/app/component/Layout.tsx
+++ b/js/app/packages/app/component/Layout.tsx
@@ -10,7 +10,7 @@ import {
import { type RouteSectionProps, useLocation } from '@solidjs/router';
import { attachGlobalDOMScope } from 'core/hotkey/hotkeys';
import { createEffect, onMount, Show, Suspense } from 'solid-js';
-import { updateCookie } from '../util/updateCookie';
+import { updateCookie } from '@core/util/cookies';
import Banner from './banner/Banner';
import { GlobalBulkEditEntityModal } from './bulk-edit-entity/BulkEditEntityModal';
import { KommandMenu } from './command/Konsole';
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsx
index e5e7aef6b7..272422624f 100644
--- a/js/app/packages/app/component/Root.tsx
+++ b/js/app/packages/app/component/Root.tsx
@@ -66,9 +66,9 @@ import {
} from '../../block-theme/utils/themeUtils';
import { TauriRouteListener } from '../../tauri/src/TauriProvider';
import { useSoundHover } from '../util/soundHover';
-import { updateCookie } from '../util/updateCookie';
+import { getLoginCookieOptions, updateCookie } from '@core/util/cookies';
import { Login } from './auth/Login';
-import { LOGIN_COOKIE_AGE, setCookie } from './auth/Shared';
+import { setCookie } from './auth/Shared';
import { makeEmailAuthComponents } from './EmailAuth';
import { GlobalAppStateProvider } from './GlobalAppState';
import { Layout } from './Layout';
@@ -81,6 +81,20 @@ import { setOpenWhichKey, WhichKey } from './WhichKey';
const { track, identify, TrackingEvents } = withAnalytics();
+/** Syncs login cookie with auth state. Only updates on successful query (not errors/loading). */
+function useSyncLoginCookie() {
+ const userInfoQuery = useUserInfoQuery();
+
+ createEffect(() => {
+ if (!userInfoQuery.isSuccess) return;
+
+ const { value, ...options } = getLoginCookieOptions(
+ userInfoQuery.data.authenticated ?? false
+ );
+ updateCookie('login', value, options);
+ });
+}
+
const rootPreload: RoutePreloadFunc = async (args) => {
useObserveRouting();
@@ -288,36 +302,10 @@ export function ConfiguredGlobalAppStateProvider(props: ParentProps) {
/** Sets user info for observability, analytics, and login cookie. Must be inside QueryClientProvider. */
function UserInfoSideEffects() {
- const userInfoQuery = useUserInfoQuery();
-
- // Set login cookie based on auth state
- createEffect(() => {
- if (userInfoQuery.isLoading) return;
- const isAuth = userInfoQuery.data?.authenticated ?? false;
-
- if (isAuth) {
- const currentDate = new Date();
- const oneMonthFromNow = new Date(
- currentDate.setMonth(currentDate.getMonth() + 1)
- );
-
- updateCookie('login', 'true', {
- expires: oneMonthFromNow,
- maxAge: LOGIN_COOKIE_AGE,
- path: '/',
- sameSite: 'Lax',
- });
- } else {
- updateCookie('login', 'false', {
- expires: new Date(0),
- maxAge: 0,
- path: '/',
- sameSite: 'Lax',
- });
- }
- });
+ useSyncLoginCookie();
// Set user info for observability and analytics
+ const userInfoQuery = useUserInfoQuery();
createEffect(() => {
if (userInfoQuery.isLoading) return;
const data = userInfoQuery.data;
diff --git a/js/app/packages/app/component/auth/Login.tsx b/js/app/packages/app/component/auth/Login.tsx
index 69f8144f0b..b4723479e9 100644
--- a/js/app/packages/app/component/auth/Login.tsx
+++ b/js/app/packages/app/component/auth/Login.tsx
@@ -15,7 +15,7 @@ import {
Show,
Switch,
} from 'solid-js';
-import { updateCookie } from '../../util/updateCookie';
+import { updateCookie } from '@core/util/cookies';
import { EmailForm } from './EmailForm';
import { LoginOptions } from './LoginOptions';
import { Input, identifyUser, Stage } from './Shared';
diff --git a/js/app/packages/app/component/auth/Shared.tsx b/js/app/packages/app/component/auth/Shared.tsx
index c184216953..c70432985f 100644
--- a/js/app/packages/app/component/auth/Shared.tsx
+++ b/js/app/packages/app/component/auth/Shared.tsx
@@ -15,8 +15,6 @@ export function setCookie(name: string, value: string, days: number) {
document.cookie = `${name}=${encodeURIComponent(value)}${expires}; path=/`;
}
-export const LOGIN_COOKIE_AGE = 2592000; // 1 month in seconds
-
export const identifyUser = async () => {
invalidateOrganization();
await invalidateUserInfo();
diff --git a/js/app/packages/app/util/updateCookie.ts b/js/app/packages/app/util/updateCookie.ts
deleted file mode 100644
index 0d6fb03d7b..0000000000
--- a/js/app/packages/app/util/updateCookie.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-export function updateCookie(
- name: string,
- value: string,
- options: { expires?: Date; maxAge?: number; path?: string; sameSite?: string }
-) {
- let newCookie = `${name}=${value}`;
-
- if (options.expires) {
- newCookie += `; expires=${options.expires.toUTCString()}`;
- }
- if (typeof options.maxAge !== 'undefined') {
- newCookie += `; max-age=${options.maxAge}`;
- }
- if (options.path) {
- newCookie += `; path=${options.path}`;
- }
- if (options.sameSite) {
- newCookie += `; SameSite=${options.sameSite}`;
- }
-
- document.cookie = newCookie;
-}
diff --git a/js/app/packages/core/util/cookies.ts b/js/app/packages/core/util/cookies.ts
index e8e88add10..a3bc5bca03 100644
--- a/js/app/packages/core/util/cookies.ts
+++ b/js/app/packages/core/util/cookies.ts
@@ -1,7 +1,21 @@
-/**
- * Check if the user appears to be authenticated based on the login cookie.
- * This prevents making auth requests during unauthenticated flows (like signup).
- */
+export const LOGIN_COOKIE_AGE = 2592000; // 1 month in seconds
+
+export type CookieOptions = {
+ expires?: Date;
+ maxAge?: number;
+ path?: string;
+ sameSite?: string;
+};
+
+export type LoginCookieOptions = {
+ value: 'true' | 'false';
+ expires: Date;
+ maxAge: number;
+ path: '/';
+ sameSite: 'Lax';
+};
+
+/** Check if the user appears to be authenticated based on the login cookie. */
export function hasLoginCookie(): boolean {
if (typeof document === 'undefined') return false;
const cookies = document.cookie.split(';');
@@ -13,3 +27,50 @@ export function hasLoginCookie(): boolean {
}
return false;
}
+
+/** Compute login cookie options from auth state. */
+export function getLoginCookieOptions(
+ isAuthenticated: boolean
+): LoginCookieOptions {
+ if (isAuthenticated) {
+ const expires = new Date();
+ expires.setMonth(expires.getMonth() + 1);
+ return {
+ value: 'true',
+ expires,
+ maxAge: LOGIN_COOKIE_AGE,
+ path: '/',
+ sameSite: 'Lax',
+ };
+ }
+ return {
+ value: 'false',
+ expires: new Date(0),
+ maxAge: 0,
+ path: '/',
+ sameSite: 'Lax',
+ };
+}
+
+export function updateCookie(
+ name: string,
+ value: string,
+ options: CookieOptions
+) {
+ let newCookie = `${name}=${value}`;
+
+ if (options.expires) {
+ newCookie += `; expires=${options.expires.toUTCString()}`;
+ }
+ if (typeof options.maxAge !== 'undefined') {
+ newCookie += `; max-age=${options.maxAge}`;
+ }
+ if (options.path) {
+ newCookie += `; path=${options.path}`;
+ }
+ if (options.sameSite) {
+ newCookie += `; SameSite=${options.sameSite}`;
+ }
+
+ document.cookie = newCookie;
+}