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