Compare commits
2 Commits
52720b2e84
...
98970af6fb
@@ -21,32 +21,36 @@ 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)
|
||||
const getApi = () => {
|
||||
const api = (window as any).electronAPI
|
||||
console.log("[LoginView] getApi:", api ? Object.keys(api) : "null")
|
||||
return api
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
console.log("[LoginView] onMount, isElectronHost:", isElectronHost())
|
||||
try {
|
||||
if (isElectronHost()) {
|
||||
const userList = await safeInvoke("users:list")
|
||||
if (userList && Array.isArray(userList)) {
|
||||
setUsers(userList)
|
||||
if (userList.length > 0) {
|
||||
setUsername(userList[0].name)
|
||||
const api = getApi()
|
||||
if (api && api.listUsers) {
|
||||
const userList = await api.listUsers()
|
||||
console.log("[LoginView] listUsers result:", userList)
|
||||
if (userList && Array.isArray(userList)) {
|
||||
setUsers(userList)
|
||||
if (userList.length > 0) {
|
||||
setUsername(userList[0].name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error("[LoginView] listUsers method not found on API")
|
||||
toast.error("API bridge incomplete")
|
||||
}
|
||||
} else {
|
||||
setUsername("web-explorer")
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch users:", error)
|
||||
toast.error("Failed to load identities")
|
||||
} finally {
|
||||
setIsLoadingUsers(false)
|
||||
}
|
||||
@@ -55,6 +59,7 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
||||
const handleLogin = async (e: Event) => {
|
||||
e.preventDefault()
|
||||
const name = username().trim()
|
||||
console.log("[LoginView] handleLogin called, name:", name)
|
||||
if (!name) {
|
||||
toast.error("Identity required")
|
||||
return
|
||||
@@ -62,13 +67,22 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
||||
|
||||
setIsLoggingIn(true)
|
||||
try {
|
||||
console.log("[LoginView] isElectronHost:", isElectronHost())
|
||||
if (isElectronHost()) {
|
||||
const userList = await safeInvoke("users:list")
|
||||
const api = getApi()
|
||||
if (!api || !api.listUsers || !api.loginUser) {
|
||||
toast.error("API bridge not ready")
|
||||
return
|
||||
}
|
||||
|
||||
console.log("[LoginView] Fetching users...")
|
||||
const userList = await api.listUsers()
|
||||
if (!userList || !Array.isArray(userList)) {
|
||||
toast.error("Bridge failure: try restarting")
|
||||
return
|
||||
}
|
||||
|
||||
console.log("[LoginView] Users found:", userList.map((u: any) => u.name))
|
||||
const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase())
|
||||
|
||||
if (!user) {
|
||||
@@ -76,19 +90,22 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
||||
return
|
||||
}
|
||||
|
||||
const result = await safeInvoke("users:login", {
|
||||
console.log("[LoginView] Attempting login for:", user.id)
|
||||
const result = await api.loginUser({
|
||||
id: user.id,
|
||||
password: password(),
|
||||
})
|
||||
|
||||
console.log("[LoginView] Login result:", result)
|
||||
if (result?.success) {
|
||||
toast.success(`Welcome back, ${result.user.name}!`)
|
||||
setActiveUserId(result.user.id) // Proactively update local state
|
||||
setActiveUserId(result.user.id)
|
||||
props.onLoginSuccess(result.user)
|
||||
} else {
|
||||
toast.error("Invalid key for this identity")
|
||||
}
|
||||
} else {
|
||||
console.log("[LoginView] Web mode login")
|
||||
toast.success("Web mode access granted")
|
||||
props.onLoginSuccess({ id: "web-user", name: "Web Explorer" })
|
||||
}
|
||||
|
||||
@@ -104,14 +104,11 @@ export async function initializeUserContext(): Promise<void> {
|
||||
console.log(`[UserContext] Initializing... host=${isElectronHost()}`)
|
||||
try {
|
||||
if (isElectronHost()) {
|
||||
const api = (window as any).electronAPI || (window as any).electron
|
||||
if (api) {
|
||||
console.log(`[UserContext] Requesting active user from host IPC...`)
|
||||
const invoke = api.invoke || api.ipcRenderer?.invoke || (window as any).ipcRenderer?.invoke
|
||||
if (!invoke) throw new Error("No IPC invoke method found")
|
||||
|
||||
const binder = api.invoke ? api : (api.ipcRenderer || (window as any).ipcRenderer)
|
||||
const activeUser = await invoke.call(binder, "users:active")
|
||||
const api = (window as any).electronAPI
|
||||
if (api && api.getActiveUser) {
|
||||
console.log(`[UserContext] Requesting active user via api.getActiveUser()...`)
|
||||
const activeUser = await api.getActiveUser()
|
||||
console.log(`[UserContext] getActiveUser result:`, activeUser)
|
||||
|
||||
if (activeUser?.id) {
|
||||
console.log(`[UserContext] Host has active session: ${activeUser.id}`)
|
||||
@@ -121,7 +118,7 @@ export async function initializeUserContext(): Promise<void> {
|
||||
setActiveUserId(null)
|
||||
}
|
||||
} else {
|
||||
console.warn(`[UserContext] Electron detected but no IPC bridge found. Falling back to web mode.`)
|
||||
console.warn(`[UserContext] electronAPI.getActiveUser not found. Falling back to web mode.`)
|
||||
await handleWebInit()
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user