fix: robust login flow with toaster at root level and password reset
Some checks failed
Release Binaries / release (push) Has been cancelled

This commit is contained in:
Gemini AI
2025-12-29 02:46:53 +04:00
Unverified
parent 0e540f2dd6
commit 52720b2e84
3 changed files with 67 additions and 54 deletions

View File

@@ -470,21 +470,6 @@ const App: Component = () => {
<RemoteAccessOverlay open={remoteAccessOpen()} onClose={() => setRemoteAccessOpen(false)} />
<AlertDialog />
<Toaster
position="top-right"
gutter={8}
containerClassName=""
containerStyle={{}}
toastOptions={{
className: "",
duration: 5000,
style: {
background: "#363636",
color: "#fff",
},
}}
/>
</div>
</>
)

View File

@@ -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<LoginViewProps> = (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<LoginViewProps> = (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<LoginViewProps> = (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)
}

View File

@@ -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 (
<Show when={isInitialized()}>
<Show
when={isLoggedIn()}
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
>
<ConfigProvider>
<InstanceConfigProvider>
<ThemeProvider>
<App />
</ThemeProvider>
</InstanceConfigProvider>
</ConfigProvider>
<>
<Toaster
position="top-right"
gutter={8}
toastOptions={{
style: {
background: "#1a1a1a",
color: "#fff",
border: "1px solid rgba(255,255,255,0.1)",
},
}}
/>
<Show when={isInitialized()}>
<Show
when={isLoggedIn()}
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
>
<ConfigProvider>
<InstanceConfigProvider>
<ThemeProvider>
<App />
</ThemeProvider>
</InstanceConfigProvider>
</ConfigProvider>
</Show>
</Show>
</Show>
</>
)
}