From 52720b2e8408f94ed807bc719986c66f916b232f Mon Sep 17 00:00:00 2001 From: Gemini AI Date: Mon, 29 Dec 2025 02:46:53 +0400 Subject: [PATCH] fix: robust login flow with toaster at root level and password reset --- packages/ui/src/App.tsx | 15 ----- packages/ui/src/components/auth/LoginView.tsx | 66 +++++++++++-------- packages/ui/src/main.tsx | 40 +++++++---- 3 files changed, 67 insertions(+), 54 deletions(-) diff --git a/packages/ui/src/App.tsx b/packages/ui/src/App.tsx index d6adb17..112b828 100644 --- a/packages/ui/src/App.tsx +++ b/packages/ui/src/App.tsx @@ -470,21 +470,6 @@ const App: Component = () => { setRemoteAccessOpen(false)} /> - - ) diff --git a/packages/ui/src/components/auth/LoginView.tsx b/packages/ui/src/components/auth/LoginView.tsx index cead507..9c43346 100644 --- a/packages/ui/src/components/auth/LoginView.tsx +++ b/packages/ui/src/components/auth/LoginView.tsx @@ -2,6 +2,7 @@ import { Component, createSignal, onMount, For, Show } from "solid-js" import { Lock, User, LogIn, ShieldCheck, Cpu } from "lucide-solid" import toast from "solid-toast" import { isElectronHost } from "../../lib/runtime-env" +import { setActiveUserId } from "../../lib/user-context" interface UserRecord { id: string @@ -20,14 +21,23 @@ const LoginView: Component = (props) => { const [isLoggingIn, setIsLoggingIn] = createSignal(false) const [isLoadingUsers, setIsLoadingUsers] = createSignal(true) + const getApi = () => (window as any).electronAPI || (window as any).electron + + const safeInvoke = async (method: string, ...args: any[]) => { + const api = getApi() + if (!api) return null + const invoke = api.invoke || api.ipcRenderer?.invoke || (window as any).ipcRenderer?.invoke + if (!invoke) return null + const binder = api.invoke ? api : (api.ipcRenderer || (window as any).ipcRenderer) + return await invoke.call(binder, method, ...args) + } + onMount(async () => { try { if (isElectronHost()) { - const api = (window as any).electronAPI - if (api) { - const userList = await api.invoke("users:list") + const userList = await safeInvoke("users:list") + if (userList && Array.isArray(userList)) { setUsers(userList) - // Pre-fill first user if available if (userList.length > 0) { setUsername(userList[0].name) } @@ -45,34 +55,38 @@ const LoginView: Component = (props) => { const handleLogin = async (e: Event) => { e.preventDefault() const name = username().trim() - if (!name) return + if (!name) { + toast.error("Identity required") + return + } setIsLoggingIn(true) try { if (isElectronHost()) { - const api = (window as any).electronAPI - if (api) { - // Find user ID by name first - const userList = await api.invoke("users:list") - const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase()) + const userList = await safeInvoke("users:list") + if (!userList || !Array.isArray(userList)) { + toast.error("Bridge failure: try restarting") + return + } - if (!user) { - toast.error(`Identity "${name}" not found`) - setIsLoggingIn(false) - return - } + const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase()) - const result = await api.invoke("users:login", { - id: user.id, - password: password(), - }) + if (!user) { + toast.error(`Identity "${name}" not found`) + return + } - if (result.success) { - toast.success(`Welcome back, ${result.user.name}!`) - props.onLoginSuccess(result.user) - } else { - toast.error("Invalid password") - } + const result = await safeInvoke("users:login", { + id: user.id, + password: password(), + }) + + if (result?.success) { + toast.success(`Welcome back, ${result.user.name}!`) + setActiveUserId(result.user.id) // Proactively update local state + props.onLoginSuccess(result.user) + } else { + toast.error("Invalid key for this identity") } } else { toast.success("Web mode access granted") @@ -80,7 +94,7 @@ const LoginView: Component = (props) => { } } catch (error) { console.error("Login failed:", error) - toast.error("Login failed. Please try again.") + toast.error("Decryption failed: check your key") } finally { setIsLoggingIn(false) } diff --git a/packages/ui/src/main.tsx b/packages/ui/src/main.tsx index 2c5e1ff..16c0617 100644 --- a/packages/ui/src/main.tsx +++ b/packages/ui/src/main.tsx @@ -7,6 +7,7 @@ import { InstanceConfigProvider } from "./stores/instance-config" import { runtimeEnv } from "./lib/runtime-env" import LoginView from "./components/auth/LoginView" import { isLoggedIn, initializeUserContext, patchFetch, isInitialized } from "./lib/user-context" +import { Toaster } from "solid-toast" import "./index.css" import "@git-diff-view/solid/styles/diff-view-pure.css" @@ -28,20 +29,33 @@ const Root = () => { }) return ( - - initializeUserContext()} />} - > - - - - - - - + <> + + + initializeUserContext()} />} + > + + + + + + + + - + ) }