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; };