Skip to content

Commit cc4f078

Browse files
author
DaneelOlivaw1
committed
fix(desktop): pass credentials when checking default server health on startup
- Store full server config (url + username + password) instead of just URL - Pass credentials to checkHealthOrAskRetry during startup - Update frontend to save complete server config when setting default - Add backward compatibility for existing URL-only storage Fixes anomalyco#16123
1 parent f363904 commit cc4f078

6 files changed

Lines changed: 74 additions & 29 deletions

File tree

‎packages/app/src/components/dialog-select-server.tsx‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ function useDefaultServer(platform: ReturnType<typeof usePlatform>, language: Re
4545
const [defaultUrl, defaultUrlActions] = createResource(
4646
async () => {
4747
try {
48-
const url = await platform.getDefaultServerUrl?.()
49-
if (!url) return null
50-
return normalizeServerUrl(url) ?? null
48+
const config = await platform.getDefaultServerUrl?.()
49+
if (!config) return null
50+
return config.url
5151
} catch (err) {
5252
showRequestError(language, err)
5353
return null
@@ -57,10 +57,17 @@ function useDefaultServer(platform: ReturnType<typeof usePlatform>, language: Re
5757
)
5858

5959
const canDefault = createMemo(() => !!platform.getDefaultServerUrl && !!platform.setDefaultServerUrl)
60-
const setDefault = async (url: string | null) => {
60+
const setDefault = async (conn: ServerConnection.Http | null) => {
6161
try {
62-
await platform.setDefaultServerUrl?.(url)
63-
defaultUrlActions.mutate(url)
62+
const config = conn
63+
? {
64+
url: conn.http.url,
65+
username: conn.http.username,
66+
password: conn.http.password,
67+
}
68+
: null
69+
await platform.setDefaultServerUrl?.(config)
70+
defaultUrlActions.mutate(conn?.http.url ?? null)
6471
} catch (err) {
6572
showRequestError(language, err)
6673
}
@@ -494,7 +501,8 @@ export function DialogSelectServer() {
494501

495502
async function handleRemove(url: ServerConnection.Key) {
496503
server.remove(url)
497-
if ((await platform.getDefaultServerUrl?.()) === url) {
504+
const defaultConfig = await platform.getDefaultServerUrl?.()
505+
if (defaultConfig?.url === url) {
498506
platform.setDefaultServerUrl?.(null)
499507
}
500508
}
@@ -585,7 +593,12 @@ export function DialogSelectServer() {
585593
<DropdownMenu.ItemLabel>{language.t("dialog.server.menu.edit")}</DropdownMenu.ItemLabel>
586594
</DropdownMenu.Item>
587595
<Show when={canDefault() && defaultUrl() !== i.http.url}>
588-
<DropdownMenu.Item onSelect={() => setDefault(i.http.url)}>
596+
<DropdownMenu.Item
597+
onSelect={() => {
598+
if (i.type !== "http") return
599+
setDefault(i)
600+
}}
601+
>
589602
<DropdownMenu.ItemLabel>
590603
{language.t("dialog.server.menu.default")}
591604
</DropdownMenu.ItemLabel>

‎packages/app/src/context/platform.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ type OpenFilePickerOptions = { title?: string; multiple?: boolean }
88
type SaveFilePickerOptions = { title?: string; defaultPath?: string }
99
type UpdateInfo = { updateAvailable: boolean; version?: string }
1010

11+
export type DefaultServerConfig = {
12+
url: string
13+
username?: string
14+
password?: string
15+
}
16+
1117
export type Platform = {
1218
/** Platform discriminator */
1319
platform: "web" | "desktop"
@@ -58,10 +64,10 @@ export type Platform = {
5864
fetch?: typeof fetch
5965

6066
/** Get the configured default server URL (platform-specific) */
61-
getDefaultServerUrl?(): Promise<string | null>
67+
getDefaultServerUrl?(): Promise<DefaultServerConfig | null>
6268

6369
/** Set the default server URL to use on app startup (platform-specific) */
64-
setDefaultServerUrl?(url: string | null): Promise<void> | void
70+
setDefaultServerUrl?(config: DefaultServerConfig | null): Promise<void> | void
6571

6672
/** Get the configured WSL integration (desktop only) */
6773
getWslEnabled?(): Promise<boolean>

‎packages/desktop-electron/src/main/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ function setInitStep(step: InitStep) {
128128
async function setupServerConnection(): Promise<ServerConnection> {
129129
const customUrl = await getSavedServerUrl()
130130

131-
if (customUrl && (await checkHealthOrAskRetry(customUrl))) {
132-
serverReady.resolve({ url: customUrl, password: null })
133-
return { variant: "existing", url: customUrl }
131+
if (customUrl && (await checkHealthOrAskRetry(customUrl.url, customUrl.username, customUrl.password))) {
132+
serverReady.resolve({ url: customUrl.url, password: null })
133+
return { variant: "existing", url: customUrl.url }
134134
}
135135

136136
const port = await getSidecarPort()

‎packages/desktop-electron/src/main/ipc.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import { execFile } from "node:child_process"
22
import { BrowserWindow, Notification, app, clipboard, dialog, ipcMain, shell } from "electron"
33
import type { IpcMainEvent, IpcMainInvokeEvent } from "electron"
44

5-
import type { InitStep, ServerReadyData, SqliteMigrationProgress, WslConfig } from "../preload/types"
5+
import type {
6+
InitStep,
7+
ServerReadyData,
8+
SqliteMigrationProgress,
9+
WslConfig,
10+
DefaultServerConfig,
11+
} from "../preload/types"
612
import { getStore } from "./store"
713

814
type Deps = {
915
killSidecar: () => void
1016
installCli: () => Promise<string>
1117
awaitInitialization: (sendStep: (step: InitStep) => void) => Promise<ServerReadyData>
12-
getDefaultServerUrl: () => Promise<string | null> | string | null
13-
setDefaultServerUrl: (url: string | null) => Promise<void> | void
18+
getDefaultServerUrl: () => Promise<DefaultServerConfig | null> | DefaultServerConfig | null
19+
setDefaultServerUrl: (config: DefaultServerConfig | null) => Promise<void> | void
1420
getWslConfig: () => Promise<WslConfig>
1521
setWslConfig: (config: WslConfig) => Promise<void> | void
1622
getDisplayBackend: () => Promise<string | null>
@@ -33,8 +39,8 @@ export function registerIpcHandlers(deps: Deps) {
3339
return deps.awaitInitialization(send)
3440
})
3541
ipcMain.handle("get-default-server-url", () => deps.getDefaultServerUrl())
36-
ipcMain.handle("set-default-server-url", (_event: IpcMainInvokeEvent, url: string | null) =>
37-
deps.setDefaultServerUrl(url),
42+
ipcMain.handle("set-default-server-url", (_event: IpcMainInvokeEvent, config: DefaultServerConfig | null) =>
43+
deps.setDefaultServerUrl(config),
3844
)
3945
ipcMain.handle("get-wsl-config", () => deps.getWslConfig())
4046
ipcMain.handle("set-wsl-config", (_event: IpcMainInvokeEvent, config: WslConfig) => deps.setWslConfig(config))

‎packages/desktop-electron/src/main/server.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@ export type WslConfig = { enabled: boolean }
88

99
export type HealthCheck = { wait: Promise<void> }
1010

11-
export function getDefaultServerUrl(): string | null {
11+
export type DefaultServerConfig = {
12+
url: string
13+
username?: string
14+
password?: string
15+
}
16+
17+
export function getDefaultServerUrl(): DefaultServerConfig | null {
1218
const value = store.get(DEFAULT_SERVER_URL_KEY)
13-
return typeof value === "string" ? value : null
19+
if (typeof value === "string") {
20+
return { url: value }
21+
}
22+
if (value && typeof value === "object" && "url" in value) {
23+
return value as DefaultServerConfig
24+
}
25+
return null
1426
}
1527

16-
export function setDefaultServerUrl(url: string | null) {
17-
if (url) {
18-
store.set(DEFAULT_SERVER_URL_KEY, url)
28+
export function setDefaultServerUrl(config: DefaultServerConfig | null) {
29+
if (config) {
30+
store.set(DEFAULT_SERVER_URL_KEY, config)
1931
return
2032
}
2133

@@ -31,13 +43,14 @@ export function setWslConfig(config: WslConfig) {
3143
store.set(WSL_ENABLED_KEY, config.enabled)
3244
}
3345

34-
export async function getSavedServerUrl(): Promise<string | null> {
46+
export async function getSavedServerUrl(): Promise<DefaultServerConfig | null> {
3547
const direct = getDefaultServerUrl()
3648
if (direct) return direct
3749

3850
const config = await getConfig().catch(() => null)
3951
if (!config) return null
40-
return getServerUrlFromConfig(config)
52+
const url = getServerUrlFromConfig(config)
53+
return url ? { url } : null
4154
}
4255

4356
export function spawnLocalServer(hostname: string, port: number, password: string) {
@@ -94,9 +107,10 @@ export async function checkHealth(url: string, password?: string | null): Promis
94107
}
95108
}
96109

97-
export async function checkHealthOrAskRetry(url: string): Promise<boolean> {
110+
export async function checkHealthOrAskRetry(url: string, username?: string, password?: string): Promise<boolean> {
98111
while (true) {
99-
if (await checkHealth(url)) return true
112+
const auth = username && password ? Buffer.from(`${username}:${password}`).toString("base64") : password
113+
if (await checkHealth(url, auth)) return true
100114

101115
const result = await dialog.showMessageBox({
102116
type: "warning",

‎packages/desktop-electron/src/preload/types.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ export type SqliteMigrationProgress = { type: "InProgress"; value: number } | {
99

1010
export type WslConfig = { enabled: boolean }
1111

12+
export type DefaultServerConfig = {
13+
url: string
14+
username?: string
15+
password?: string
16+
}
17+
1218
export type LinuxDisplayBackend = "wayland" | "auto"
1319

1420
export type ElectronAPI = {
1521
killSidecar: () => Promise<void>
1622
installCli: () => Promise<string>
1723
awaitInitialization: (onStep: (step: InitStep) => void) => Promise<ServerReadyData>
18-
getDefaultServerUrl: () => Promise<string | null>
19-
setDefaultServerUrl: (url: string | null) => Promise<void>
24+
getDefaultServerUrl: () => Promise<DefaultServerConfig | null>
25+
setDefaultServerUrl: (config: DefaultServerConfig | null) => Promise<void>
2026
getWslConfig: () => Promise<WslConfig>
2127
setWslConfig: (config: WslConfig) => Promise<void>
2228
getDisplayBackend: () => Promise<LinuxDisplayBackend | null>

0 commit comments

Comments
 (0)