fix(desktop): restore renderer api requests

This commit is contained in:
DIYgod 2026-03-12 20:35:26 +08:00
parent 60e2dd3198
commit 94f90873d5
3 changed files with 6 additions and 126 deletions

View File

@ -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<string, string>
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,

View File

@ -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<typeof ipcServices>["auth"] & {
request?: (payload: {
input: string
init?: { method?: string; headers?: Record<string, string>; body?: string }
}) => Promise<{ status: number; headers: Record<string, string>; 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

View File

@ -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<typeof followApi.auth.getSession>) => {
const authService = ipcServices?.auth as
| (typeof followApi.auth & {
getSession?: () => ReturnType<typeof followApi.auth.getSession>
getSessionByToken?: (token: string) => ReturnType<typeof followApi.auth.getSession>
})
| 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 }) => {