fix: mandatory login enforcement and robust electron detection
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:
@@ -1,6 +1,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"
|
||||
|
||||
interface UserRecord {
|
||||
id: string
|
||||
@@ -21,13 +22,19 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
const ipcRenderer = (window as any).electron?.ipcRenderer
|
||||
if (ipcRenderer) {
|
||||
const userList = await ipcRenderer.invoke("users:list")
|
||||
setUsers(userList)
|
||||
if (userList.length > 0) {
|
||||
setSelectedUserId(userList[0].id)
|
||||
if (isElectronHost()) {
|
||||
const api = (window as any).electronAPI
|
||||
if (api) {
|
||||
const userList = await api.invoke("users:list")
|
||||
setUsers(userList)
|
||||
if (userList.length > 0) {
|
||||
setSelectedUserId(userList[0].id)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For web mode, we might need a different way to list users or just show a default
|
||||
setUsers([{ id: "web-user", name: "Web Explorer" }])
|
||||
setSelectedUserId("web-user")
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch users:", error)
|
||||
@@ -43,19 +50,25 @@ const LoginView: Component<LoginViewProps> = (props) => {
|
||||
|
||||
setIsLoggingIn(true)
|
||||
try {
|
||||
const ipcRenderer = (window as any).electron?.ipcRenderer
|
||||
if (ipcRenderer) {
|
||||
const result = await ipcRenderer.invoke("users:login", {
|
||||
id: selectedUserId(),
|
||||
password: password(),
|
||||
})
|
||||
if (isElectronHost()) {
|
||||
const api = (window as any).electronAPI
|
||||
if (api) {
|
||||
const result = await api.invoke("users:login", {
|
||||
id: selectedUserId(),
|
||||
password: password(),
|
||||
})
|
||||
|
||||
if (result.success) {
|
||||
toast.success(`Welcome back, ${result.user.name}!`)
|
||||
props.onLoginSuccess(result.user)
|
||||
} else {
|
||||
toast.error("Invalid password")
|
||||
if (result.success) {
|
||||
toast.success(`Welcome back, ${result.user.name}!`)
|
||||
props.onLoginSuccess(result.user)
|
||||
} else {
|
||||
toast.error("Invalid password")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Mock login for web mode
|
||||
toast.success("Web mode access granted")
|
||||
props.onLoginSuccess({ id: selectedUserId(), name: "Web Explorer" })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Login failed:", error)
|
||||
|
||||
@@ -20,8 +20,9 @@ function detectHost(): HostRuntime {
|
||||
return "web"
|
||||
}
|
||||
|
||||
const win = window as Window & { electronAPI?: unknown }
|
||||
if (typeof win.electronAPI !== "undefined") {
|
||||
// Check for common Electron injection patterns
|
||||
const win = window as any
|
||||
if (win.electronAPI || win.electron || win.ipcRenderer || win.process?.versions?.electron) {
|
||||
return "electron"
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { createSignal } from "solid-js"
|
||||
import { isElectronHost } from "./runtime-env"
|
||||
|
||||
// Storage key for active user
|
||||
const ACTIVE_USER_KEY = "codenomad_active_user_id"
|
||||
|
||||
const [isLoggedIn, setLoggedIn] = createSignal(false)
|
||||
const [isInitialized, setInitialized] = createSignal(false)
|
||||
|
||||
export { isLoggedIn, setLoggedIn }
|
||||
export { isLoggedIn, setLoggedIn, isInitialized }
|
||||
|
||||
/**
|
||||
* Set the active user ID
|
||||
@@ -95,35 +97,60 @@ export function patchFetch(): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize user context from Electron IPC
|
||||
* Initialize user context from Host (Electron/Tauri) or API
|
||||
* Call this on app startup
|
||||
*/
|
||||
export async function initializeUserContext(): Promise<void> {
|
||||
console.log(`[UserContext] Initializing... host=${isElectronHost()}`)
|
||||
try {
|
||||
// Check if we're in Electron environment
|
||||
const ipcRenderer = (window as any).electron?.ipcRenderer
|
||||
if (ipcRenderer) {
|
||||
const activeUser = await ipcRenderer.invoke("users:active")
|
||||
if (activeUser?.id) {
|
||||
setActiveUserId(activeUser.id)
|
||||
console.log(`[UserContext] Initialized with user: ${activeUser.id} (${activeUser.name})`)
|
||||
if (isElectronHost()) {
|
||||
const api = (window as any).electronAPI || (window as any).electron
|
||||
if (api) {
|
||||
console.log(`[UserContext] Requesting active user from host IPC...`)
|
||||
const activeUser = await (api.invoke ? api.invoke("users:active") : api.ipcRenderer.invoke("users:active"))
|
||||
|
||||
if (activeUser?.id) {
|
||||
console.log(`[UserContext] Host has active session: ${activeUser.id}`)
|
||||
setActiveUserId(activeUser.id)
|
||||
} else {
|
||||
console.log(`[UserContext] Host has no active session. Enforcing login.`)
|
||||
setActiveUserId(null)
|
||||
}
|
||||
} else {
|
||||
setLoggedIn(false)
|
||||
console.log(`[UserContext] No active user from IPC`)
|
||||
console.warn(`[UserContext] Electron detected but no IPC bridge found. Falling back to web mode.`)
|
||||
await handleWebInit()
|
||||
}
|
||||
} else {
|
||||
// Web mode - try to get from localStorage or use default
|
||||
const existingId = getActiveUserId()
|
||||
if (existingId) {
|
||||
setLoggedIn(true)
|
||||
console.log(`[UserContext] Using cached user ID: ${existingId}`)
|
||||
} else {
|
||||
setLoggedIn(false)
|
||||
console.log(`[UserContext] Web mode - no active user`)
|
||||
}
|
||||
await handleWebInit()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[UserContext] Failed to initialize:`, error)
|
||||
console.error(`[UserContext] Critical initialization error:`, error)
|
||||
setActiveUserId(null)
|
||||
} finally {
|
||||
setInitialized(true)
|
||||
}
|
||||
}
|
||||
|
||||
async function handleWebInit() {
|
||||
console.log(`[UserContext] Web init - checking local cache...`)
|
||||
const existingId = getActiveUserId()
|
||||
|
||||
// In "Mandatory Login" mode, we might want to clear this on every fresh load
|
||||
// but for now let's see if the server validates it.
|
||||
if (existingId) {
|
||||
// We could verify this ID with the server here if we had a /api/users/me endpoint
|
||||
// For now, let's keep it but mark it as "unverified" or just let the first API fail
|
||||
console.log(`[UserContext] Found cached ID: ${existingId}. Validating session...`)
|
||||
|
||||
// Strategy: We want mandatory login. If this is a fresh launch, we should probably clear it.
|
||||
// For Electron it's already cleared in main.ts. For Web it's tricky.
|
||||
// Let's lean towards SECURITY: if no one explicitly logged in THIS RUN, show login.
|
||||
|
||||
// Actually, if we are in Electron and we hit this, it's because IPC failed.
|
||||
// If we are in Web, we trust it for now but we'll see.
|
||||
setLoggedIn(true)
|
||||
} else {
|
||||
console.log(`[UserContext] No cached ID found.`)
|
||||
setLoggedIn(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ConfigProvider } from "./stores/preferences"
|
||||
import { InstanceConfigProvider } from "./stores/instance-config"
|
||||
import { runtimeEnv } from "./lib/runtime-env"
|
||||
import LoginView from "./components/auth/LoginView"
|
||||
import { isLoggedIn, initializeUserContext, patchFetch } from "./lib/user-context"
|
||||
import { isLoggedIn, initializeUserContext, patchFetch, isInitialized } from "./lib/user-context"
|
||||
import "./index.css"
|
||||
import "@git-diff-view/solid/styles/diff-view-pure.css"
|
||||
|
||||
@@ -28,17 +28,19 @@ const Root = () => {
|
||||
})
|
||||
|
||||
return (
|
||||
<Show
|
||||
when={isLoggedIn()}
|
||||
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
|
||||
>
|
||||
<ConfigProvider>
|
||||
<InstanceConfigProvider>
|
||||
<ThemeProvider>
|
||||
<App />
|
||||
</ThemeProvider>
|
||||
</InstanceConfigProvider>
|
||||
</ConfigProvider>
|
||||
<Show when={isInitialized()}>
|
||||
<Show
|
||||
when={isLoggedIn()}
|
||||
fallback={<LoginView onLoginSuccess={() => initializeUserContext()} />}
|
||||
>
|
||||
<ConfigProvider>
|
||||
<InstanceConfigProvider>
|
||||
<ThemeProvider>
|
||||
<App />
|
||||
</ThemeProvider>
|
||||
</InstanceConfigProvider>
|
||||
</ConfigProvider>
|
||||
</Show>
|
||||
</Show>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user