SurfSense/surfsense_web/app/dashboard/layout.tsx
DESKTOP-RTLN3BA\$punk e920923fa4 feat: implement auth token synchronization between Electron and web app
- Added IPC channels for getting and setting auth tokens in Electron.
- Implemented functions to sync tokens from localStorage to Electron and vice versa.
- Updated components to ensure tokens are retrieved from Electron when not available locally.
- Enhanced user authentication flow by integrating token management across windows.
2026-04-06 23:02:25 -07:00

46 lines
1.2 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { USER_QUERY_KEY } from "@/atoms/user/user-query.atoms";
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
import { getBearerToken, ensureTokensFromElectron, redirectToLogin } from "@/lib/auth-utils";
import { queryClient } from "@/lib/query-client/client";
interface DashboardLayoutProps {
children: React.ReactNode;
}
export default function DashboardLayout({ children }: DashboardLayoutProps) {
const [isCheckingAuth, setIsCheckingAuth] = useState(true);
// Use the global loading screen - spinner animation won't reset
useGlobalLoadingEffect(isCheckingAuth);
useEffect(() => {
async function checkAuth() {
let token = getBearerToken();
if (!token) {
const synced = await ensureTokensFromElectron();
if (synced) token = getBearerToken();
}
if (!token) {
redirectToLogin();
return;
}
queryClient.invalidateQueries({ queryKey: [...USER_QUERY_KEY] });
setIsCheckingAuth(false);
}
checkAuth();
}, []);
// Return null while loading - the global provider handles the loading UI
if (isCheckingAuth) {
return null;
}
return (
<div className="h-full flex flex-col ">
<div className="flex-1 min-h-0">{children}</div>
</div>
);
}