Merge pull request #963 from Mail-0/dakdevs/staging/broken-connections-fix

Fix redirection for Invalid Connections
This commit is contained in:
Adam
2025-05-12 20:27:01 -07:00
committed by GitHub
2 changed files with 15 additions and 17 deletions

View File

@@ -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<AppRouter>({
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;
}),
}),

View File

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