From 94f90873d587e8fdff6259dd65a845e638d11c0a Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 12 Mar 2026 20:35:26 +0800 Subject: [PATCH] fix(desktop): restore renderer api requests --- .../layer/main/src/ipc/services/auth.ts | 44 --------------- .../layer/renderer/src/lib/api-client.ts | 56 ++----------------- apps/desktop/layer/renderer/src/main.tsx | 32 +---------- 3 files changed, 6 insertions(+), 126 deletions(-) diff --git a/apps/desktop/layer/main/src/ipc/services/auth.ts b/apps/desktop/layer/main/src/ipc/services/auth.ts index aae7c1813..fb6778774 100644 --- a/apps/desktop/layer/main/src/ipc/services/auth.ts +++ b/apps/desktop/layer/main/src/ipc/services/auth.ts @@ -5,7 +5,6 @@ import type { IpcContext } from "electron-ipc-decorator" import { IpcMethod, IpcService } from "electron-ipc-decorator" import { BETTER_AUTH_COOKIE_NAME_SESSION_TOKEN } from "~/constants/app" -import { apiClient } from "~/lib/api-client" import { WindowManager } from "~/manager/window" import { getSessionTokenFromCookies, syncSessionToCliConfig } from "../../lib/cli-session-sync" @@ -135,49 +134,6 @@ export class AuthService extends IpcService { await this.clearSessionToken() } - @IpcMethod() - async getSession(_context: IpcContext) { - return apiClient.auth.getSession() - } - - @IpcMethod() - async getSessionByToken(_context: IpcContext, token: string) { - const response = await fetch(`${env.VITE_API_URL}/better-auth/get-session`, { - headers: { - ...createDesktopAPIHeaders({ version: PKG.version }), - Cookie: `__Secure-better-auth.session_token=${token}; better-auth.session_token=${token}`, - }, - }) - - return response.json().catch(async () => ({ message: await response.text() })) - } - - @IpcMethod() - async request( - _context: IpcContext, - payload: { - input: string - init?: { - method?: string - headers?: Record - body?: string - } - }, - ) { - const response = await fetch(payload.input, { - method: payload.init?.method, - headers: payload.init?.headers, - body: payload.init?.body, - cache: "no-store", - }) - - return { - status: response.status, - headers: Object.fromEntries(response.headers.entries()), - body: await response.text(), - } - } - @IpcMethod() async signInWithCredential( _context: IpcContext, diff --git a/apps/desktop/layer/renderer/src/lib/api-client.ts b/apps/desktop/layer/renderer/src/lib/api-client.ts index aed9d5d0f..25102bef8 100644 --- a/apps/desktop/layer/renderer/src/lib/api-client.ts +++ b/apps/desktop/layer/renderer/src/lib/api-client.ts @@ -1,4 +1,3 @@ -import { IN_ELECTRON } from "@follow/shared/constants" import { env } from "@follow/shared/env.desktop" import { whoami } from "@follow/store/user/getters" import { userActions } from "@follow/store/user/store" @@ -9,53 +8,17 @@ import PKG from "@pkg" import { NetworkStatus, setApiStatus } from "~/atoms/network" import { setLoginModalShow } from "~/atoms/user" -import { ipcServices } from "./client" -import { getAuthSessionToken, getClientId, getSessionId } from "./client-session" - -const electronFetch = async (input: string | URL | Request, options: RequestInit = {}) => { - const authService = ipcServices?.auth as - | (NonNullable["auth"] & { - request?: (payload: { - input: string - init?: { method?: string; headers?: Record; body?: string } - }) => Promise<{ status: number; headers: Record; body: string }> - }) - | undefined - - if (!authService?.request) { - return fetch(input.toString(), { - ...options, - cache: "no-store", - }) - } - - const headers = new Headers(options.headers) - const response = await authService.request({ - input: input.toString(), - init: { - method: options.method, - headers: Object.fromEntries(headers.entries()), - body: typeof options.body === "string" ? options.body : undefined, - }, - }) - - return new Response(response.body, { - status: response.status, - headers: response.headers, - }) -} +import { getClientId, getSessionId } from "./client-session" export const followClient = new FollowClient({ credentials: "include", timeout: 30000, baseURL: env.VITE_API_URL, fetch: async (input, options = {}) => - IN_ELECTRON - ? electronFetch(input, options) - : fetch(input.toString(), { - ...options, - cache: "no-store", - }), + fetch(input.toString(), { + ...options, + cache: "no-store", + }), }) export const followApi = followClient.api @@ -65,11 +28,6 @@ followClient.addRequestInterceptor(async (ctx) => { header["X-Client-Id"] = getClientId() header["X-Session-Id"] = getSessionId() - const authSessionToken = IN_ELECTRON ? getAuthSessionToken() : null - if (authSessionToken) { - header.Cookie = `__Secure-better-auth.session_token=${authSessionToken}; better-auth.session_token=${authSessionToken}` - } - const apiHeader = createDesktopAPIHeaders({ version: PKG.version }) options.headers = { @@ -107,10 +65,6 @@ followClient.addResponseInterceptor(async ({ response }) => { return response } - if (IN_ELECTRON && getAuthSessionToken()) { - return response - } - // Or we can present LoginModal here. // router.navigate("/login") // If any response status is 401, we can set auth fail. Maybe some bug, but if navigate to login page, had same issues diff --git a/apps/desktop/layer/renderer/src/main.tsx b/apps/desktop/layer/renderer/src/main.tsx index d22eb2127..e09c49b0c 100644 --- a/apps/desktop/layer/renderer/src/main.tsx +++ b/apps/desktop/layer/renderer/src/main.tsx @@ -11,8 +11,6 @@ import ReactDOM from "react-dom/client" import { RouterProvider } from "react-router/dom" import { authClient } from "~/lib/auth" -import { ipcServices } from "~/lib/client" -import { getAuthSessionToken } from "~/lib/client-session" import { setAppIsReady } from "./atoms/app" import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT } from "./constants" @@ -24,35 +22,7 @@ import { router } from "./router" authClientContext.provide(authClient) queryClientContext.provide(queryClient) - -const providedApi = IN_ELECTRON - ? { - ...followApi, - auth: { - ...followApi.auth, - getSession: async (...args: Parameters) => { - const authService = ipcServices?.auth as - | (typeof followApi.auth & { - getSession?: () => ReturnType - getSessionByToken?: (token: string) => ReturnType - }) - | undefined - const authSessionToken = getAuthSessionToken() - const session = authSessionToken - ? await authService?.getSessionByToken?.(authSessionToken) - : await authService?.getSession?.() - - if (session) { - return session - } - - return followApi.auth.getSession(...args) - }, - }, - } - : followApi - -apiContext.provide(providedApi) +apiContext.provide(followApi) initializeApp().finally(() => { import("./push-notification").then(({ registerWebPushNotifications }) => {