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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user