From 6936dd008d37b677126f5704307a9529970bd4bb Mon Sep 17 00:00:00 2001 From: Dak Washbrook Date: Mon, 12 May 2025 16:01:17 -0700 Subject: [PATCH 1/3] Refactor connection error handling and improve redirect logic Removed redundant checks for connection tokens and streamlined error handling in server utilities. Enhanced redirect logic in the query provider to avoid unnecessary navigation when already on the target path. --- apps/mail/providers/query-provider.tsx | 8 +++++++- apps/server/src/lib/server-utils.ts | 24 ++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/apps/mail/providers/query-provider.tsx b/apps/mail/providers/query-provider.tsx index 42b4e2462..4316491f7 100644 --- a/apps/mail/providers/query-provider.tsx +++ b/apps/mail/providers/query-provider.tsx @@ -39,6 +39,7 @@ export const makeQueryClient = (session: Session | null) => signOut({ fetchOptions: { onSuccess: () => { + if (window.location.href.includes('/login')) return; window.location.href = '/login?error=required_scopes_missing'; }, }, @@ -91,8 +92,13 @@ export const trpcClient = createTRPCClient({ methodOverride: 'POST', fetch: (url, options) => fetch(url, { ...options, credentials: 'include' }).then((res) => { + const currentPath = new URL(window.location.href).pathname; const redirectPath = res.headers.get('X-Zero-Redirect'); - if (redirectPath) window.location.href = redirectPath; + + if (!!redirectPath && redirectPath !== currentPath) { + window.location.href = redirectPath; + } + return res; }), }), diff --git a/apps/server/src/lib/server-utils.ts b/apps/server/src/lib/server-utils.ts index 73367120f..dabeee246 100644 --- a/apps/server/src/lib/server-utils.ts +++ b/apps/server/src/lib/server-utils.ts @@ -13,10 +13,6 @@ export const getActiveConnection = async (c: HonoContext) => { if (!activeConnection) throw new Error(`Active connection not found for user ${session.user.id}`); - if (!activeConnection.refreshToken || !activeConnection.accessToken) - throw new Error( - 'Active Connection is not properly authorized, please reconnect the connection', - ); return activeConnection; } @@ -29,24 +25,20 @@ export const getActiveConnection = async (c: HonoContext) => { if (!activeConnection) throw new Error('Active connection not found'); - if (!activeConnection.refreshToken || !activeConnection.accessToken) - throw new Error( - 'Active Connection is not properly authorized, please reconnect the connection', - ); return activeConnection; }; -export const connectionToDriver = ( - activeConnection: typeof connection.$inferSelect, - c: HonoContext, -) => { - const driver = createDriver(activeConnection.providerId, { +export const connectionToDriver = (activeConnection: typeof connection.$inferSelect) => { + if (!activeConnection.accessToken || !activeConnection.refreshToken) { + throw new Error('Invalid connection'); + } + + return createDriver(activeConnection.providerId, { auth: { accessToken: activeConnection.accessToken, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - refreshToken: activeConnection.refreshToken!, + + refreshToken: activeConnection.refreshToken, email: activeConnection.email, }, }); - return driver; }; From 474f3fd9591d2b8b3e8ffd7df46423da79fad70a Mon Sep 17 00:00:00 2001 From: Dak Washbrook Date: Mon, 12 May 2025 16:13:20 -0700 Subject: [PATCH 2/3] Add debug logging for redirect handling in query provider Added console logs to inspect `redirectPath`, `currentPath`, and test their states. This will help diagnose issues and ensure the redirect logic is functioning correctly. --- apps/mail/providers/query-provider.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/mail/providers/query-provider.tsx b/apps/mail/providers/query-provider.tsx index 4316491f7..6c6704386 100644 --- a/apps/mail/providers/query-provider.tsx +++ b/apps/mail/providers/query-provider.tsx @@ -95,7 +95,10 @@ export const trpcClient = createTRPCClient({ const currentPath = new URL(window.location.href).pathname; const redirectPath = res.headers.get('X-Zero-Redirect'); + console.log('redirectPath', redirectPath); + console.log('currentPath', currentPath); if (!!redirectPath && redirectPath !== currentPath) { + console.log('test'); window.location.href = redirectPath; } From d0cf71b9bbbc9b4a48bd7cf6b118a4609eec9cb6 Mon Sep 17 00:00:00 2001 From: Dak Washbrook Date: Mon, 12 May 2025 16:23:29 -0700 Subject: [PATCH 3/3] Remove redundant console.log statements in query-provider.tsx Eliminated unnecessary debugging logs to clean up the code and improve readability. This helps maintain a more production-ready codebase with fewer distractions during development. --- apps/mail/providers/query-provider.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/apps/mail/providers/query-provider.tsx b/apps/mail/providers/query-provider.tsx index 6c6704386..4316491f7 100644 --- a/apps/mail/providers/query-provider.tsx +++ b/apps/mail/providers/query-provider.tsx @@ -95,10 +95,7 @@ export const trpcClient = createTRPCClient({ const currentPath = new URL(window.location.href).pathname; const redirectPath = res.headers.get('X-Zero-Redirect'); - console.log('redirectPath', redirectPath); - console.log('currentPath', currentPath); if (!!redirectPath && redirectPath !== currentPath) { - console.log('test'); window.location.href = redirectPath; }