Compare commits

..

2 Commits

2 changed files with 40 additions and 26 deletions

View File

@@ -21,32 +21,36 @@ 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 getApi = () => {
const api = (window as any).electronAPI
const safeInvoke = async (method: string, ...args: any[]) => { console.log("[LoginView] getApi:", api ? Object.keys(api) : "null")
const api = getApi() return api
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 () => {
console.log("[LoginView] onMount, isElectronHost:", isElectronHost())
try { try {
if (isElectronHost()) { if (isElectronHost()) {
const userList = await safeInvoke("users:list") const api = getApi()
if (userList && Array.isArray(userList)) { if (api && api.listUsers) {
setUsers(userList) const userList = await api.listUsers()
if (userList.length > 0) { console.log("[LoginView] listUsers result:", userList)
setUsername(userList[0].name) 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 { } else {
setUsername("web-explorer") setUsername("web-explorer")
} }
} catch (error) { } catch (error) {
console.error("Failed to fetch users:", error) console.error("Failed to fetch users:", error)
toast.error("Failed to load identities")
} finally { } finally {
setIsLoadingUsers(false) setIsLoadingUsers(false)
} }
@@ -55,6 +59,7 @@ 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()
console.log("[LoginView] handleLogin called, name:", name)
if (!name) { if (!name) {
toast.error("Identity required") toast.error("Identity required")
return return
@@ -62,13 +67,22 @@ const LoginView: Component<LoginViewProps> = (props) => {
setIsLoggingIn(true) setIsLoggingIn(true)
try { try {
console.log("[LoginView] isElectronHost:", isElectronHost())
if (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)) { if (!userList || !Array.isArray(userList)) {
toast.error("Bridge failure: try restarting") toast.error("Bridge failure: try restarting")
return return
} }
console.log("[LoginView] Users found:", userList.map((u: any) => u.name))
const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase()) const user = userList.find((u: UserRecord) => u.name.toLowerCase() === name.toLowerCase())
if (!user) { if (!user) {
@@ -76,19 +90,22 @@ const LoginView: Component<LoginViewProps> = (props) => {
return return
} }
const result = await safeInvoke("users:login", { console.log("[LoginView] Attempting login for:", user.id)
const result = await api.loginUser({
id: user.id, id: user.id,
password: password(), password: password(),
}) })
console.log("[LoginView] Login result:", result)
if (result?.success) { if (result?.success) {
toast.success(`Welcome back, ${result.user.name}!`) toast.success(`Welcome back, ${result.user.name}!`)
setActiveUserId(result.user.id) // Proactively update local state setActiveUserId(result.user.id)
props.onLoginSuccess(result.user) props.onLoginSuccess(result.user)
} else { } else {
toast.error("Invalid key for this identity") toast.error("Invalid key for this identity")
} }
} else { } else {
console.log("[LoginView] Web mode login")
toast.success("Web mode access granted") toast.success("Web mode access granted")
props.onLoginSuccess({ id: "web-user", name: "Web Explorer" }) props.onLoginSuccess({ id: "web-user", name: "Web Explorer" })
} }

View File

@@ -104,14 +104,11 @@ export async function initializeUserContext(): Promise<void> {
console.log(`[UserContext] Initializing... host=${isElectronHost()}`) console.log(`[UserContext] Initializing... host=${isElectronHost()}`)
try { try {
if (isElectronHost()) { if (isElectronHost()) {
const api = (window as any).electronAPI || (window as any).electron const api = (window as any).electronAPI
if (api) { if (api && api.getActiveUser) {
console.log(`[UserContext] Requesting active user from host IPC...`) console.log(`[UserContext] Requesting active user via api.getActiveUser()...`)
const invoke = api.invoke || api.ipcRenderer?.invoke || (window as any).ipcRenderer?.invoke const activeUser = await api.getActiveUser()
if (!invoke) throw new Error("No IPC invoke method found") console.log(`[UserContext] getActiveUser result:`, activeUser)
const binder = api.invoke ? api : (api.ipcRenderer || (window as any).ipcRenderer)
const activeUser = await invoke.call(binder, "users:active")
if (activeUser?.id) { if (activeUser?.id) {
console.log(`[UserContext] Host has active session: ${activeUser.id}`) console.log(`[UserContext] Host has active session: ${activeUser.id}`)
@@ -121,7 +118,7 @@ export async function initializeUserContext(): Promise<void> {
setActiveUserId(null) setActiveUserId(null)
} }
} else { } 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() await handleWebInit()
} }
} else { } else {