fix: robust login flow with toaster at root level and password reset
Some checks failed
Release Binaries / release (push) Has been cancelled
Some checks failed
Release Binaries / release (push) Has been cancelled
This commit is contained in:
@@ -470,21 +470,6 @@ const App: Component = () => {
|
|||||||
<RemoteAccessOverlay open={remoteAccessOpen()} onClose={() => setRemoteAccessOpen(false)} />
|
<RemoteAccessOverlay open={remoteAccessOpen()} onClose={() => setRemoteAccessOpen(false)} />
|
||||||
|
|
||||||
<AlertDialog />
|
<AlertDialog />
|
||||||
|
|
||||||
<Toaster
|
|
||||||
position="top-right"
|
|
||||||
gutter={8}
|
|
||||||
containerClassName=""
|
|
||||||
containerStyle={{}}
|
|
||||||
toastOptions={{
|
|
||||||
className: "",
|
|
||||||
duration: 5000,
|
|
||||||
style: {
|
|
||||||
background: "#363636",
|
|
||||||
color: "#fff",
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Component, createSignal, onMount, For, Show } from "solid-js"
|
|||||||
import { Lock, User, LogIn, ShieldCheck, Cpu } from "lucide-solid"
|
import { Lock, User, LogIn, ShieldCheck, Cpu } from "lucide-solid"
|
||||||
import toast from "solid-toast"
|
import toast from "solid-toast"
|
||||||
import { isElectronHost } from "../../lib/runtime-env"
|
import { isElectronHost } from "../../lib/runtime-env"
|
||||||
|
import { setActiveUserId } from "../../lib/user-context"
|
||||||
|
|
||||||
interface UserRecord {
|
interface UserRecord {
|
||||||
id: string
|
id: string
|
||||||
@@ -20,14 +21,23 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
|||||||
const [isLoggingIn, setIsLoggingIn] = createSignal(false)
|
const [isLoggingIn, setIsLoggingIn] = createSignal(false)
|
||||||
const [isLoadingUsers, setIsLoadingUsers] = createSignal(true)
|
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 () => {
|
onMount(async () => {
|
||||||
try {
|
try {
|
||||||
if (isElectronHost()) {
|
if (isElectronHost()) {
|
||||||
const api = (window as any).electronAPI
|
const userList = await safeInvoke("users:list")
|
||||||
if (api) {
|
if (userList && Array.isArray(userList)) {
|
||||||
const userList = await api.invoke("users:list")
|
|
||||||
setUsers(userList)
|
setUsers(userList)
|
||||||
// Pre-fill first user if available
|
|
||||||
if (userList.length > 0) {
|
if (userList.length > 0) {
|
||||||
setUsername(userList[0].name)
|
setUsername(userList[0].name)
|
||||||
}
|
}
|
||||||
@@ -45,34 +55,38 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
|||||||
const handleLogin = async (e: Event) => {
|
const handleLogin = async (e: Event) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const name = username().trim()
|
const name = username().trim()
|
||||||
if (!name) return
|
if (!name) {
|
||||||
|
toast.error("Identity required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setIsLoggingIn(true)
|
setIsLoggingIn(true)
|
||||||
try {
|
try {
|
||||||
if (isElectronHost()) {
|
if (isElectronHost()) {
|
||||||
const api = (window as any).electronAPI
|
const userList = await safeInvoke("users:list")
|
||||||
if (api) {
|
if (!userList || !Array.isArray(userList)) {
|
||||||
// Find user ID by name first
|
toast.error("Bridge failure: try restarting")
|
||||||
const userList = await api.invoke("users:list")
|
return
|
||||||
const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase())
|
}
|
||||||
|
|
||||||
if (!user) {
|
const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase())
|
||||||
toast.error(`Identity "${name}" not found`)
|
|
||||||
setIsLoggingIn(false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await api.invoke("users:login", {
|
if (!user) {
|
||||||
id: user.id,
|
toast.error(`Identity "${name}" not found`)
|
||||||
password: password(),
|
return
|
||||||
})
|
}
|
||||||
|
|
||||||
if (result.success) {
|
const result = await safeInvoke("users:login", {
|
||||||
toast.success(`Welcome back, ${result.user.name}!`)
|
id: user.id,
|
||||||
props.onLoginSuccess(result.user)
|
password: password(),
|
||||||
} else {
|
})
|
||||||
toast.error("Invalid 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 {
|
} else {
|
||||||
toast.success("Web mode access granted")
|
toast.success("Web mode access granted")
|
||||||
@@ -80,7 +94,7 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Login failed:", error)
|
console.error("Login failed:", error)
|
||||||
toast.error("Login failed. Please try again.")
|
toast.error("Decryption failed: check your key")
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoggingIn(false)
|
setIsLoggingIn(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { InstanceConfigProvider } from "./stores/instance-config"
|
|||||||
import { runtimeEnv } from "./lib/runtime-env"
|
import { runtimeEnv } from "./lib/runtime-env"
|
||||||
import LoginView from "./components/auth/LoginView"
|
import LoginView from "./components/auth/LoginView"
|
||||||
import { isLoggedIn, initializeUserContext, patchFetch, isInitialized } from "./lib/user-context"
|
import { isLoggedIn, initializeUserContext, patchFetch, isInitialized } from "./lib/user-context"
|
||||||
|
import { Toaster } from "solid-toast"
|
||||||
import "./index.css"
|
import "./index.css"
|
||||||
import "@git-diff-view/solid/styles/diff-view-pure.css"
|
import "@git-diff-view/solid/styles/diff-view-pure.css"
|
||||||
|
|
||||||
@@ -28,20 +29,33 @@ const Root = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Show when={isInitialized()}>
|
<>
|
||||||
<Show
|
<Toaster
|
||||||
when={isLoggedIn()}
|
position="top-right"
|
||||||
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
|
gutter={8}
|
||||||
>
|
toastOptions={{
|
||||||
<ConfigProvider>
|
style: {
|
||||||
<InstanceConfigProvider>
|
background: "#1a1a1a",
|
||||||
<ThemeProvider>
|
color: "#fff",
|
||||||
<App />
|
border: "1px solid rgba(255,255,255,0.1)",
|
||||||
</ThemeProvider>
|
},
|
||||||
</InstanceConfigProvider>
|
}}
|
||||||
</ConfigProvider>
|
/>
|
||||||
|
<Show when={isInitialized()}>
|
||||||
|
<Show
|
||||||
|
when={isLoggedIn()}
|
||||||
|
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
|
||||||
|
>
|
||||||
|
<ConfigProvider>
|
||||||
|
<InstanceConfigProvider>
|
||||||
|
<ThemeProvider>
|
||||||
|
<App />
|
||||||
|
</ThemeProvider>
|
||||||
|
</InstanceConfigProvider>
|
||||||
|
</ConfigProvider>
|
||||||
|
</Show>
|
||||||
</Show>
|
</Show>
|
||||||
</Show>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user