diff --git a/app/providers.tsx b/app/providers.tsx index effc46c..6ea9db5 100644 --- a/app/providers.tsx +++ b/app/providers.tsx @@ -3,11 +3,34 @@ import { GlobalLoginDialog } from "@/components/global-login-dialog" import { ThemeSyncEffect } from "@/components/theme-sync" import { TooltipProvider } from "@/components/ui/tooltip" +import { getCurrentUserForLogin } from "@/lib/api" +import { useAuthStore } from "@/store/auth" import { QueryClient, QueryClientProvider } from "@tanstack/react-query" import { ThemeProvider } from "next-themes" -import { useState } from "react" +import { useEffect, useRef, useState } from "react" import { Toaster } from "sonner" +function AuthBootstrap() { + const login = useAuthStore((s) => s.login) + const isAuthenticated = useAuthStore((s) => s.isAuthenticated) + const tried = useRef(false) + + useEffect(() => { + if (tried.current || isAuthenticated) return + tried.current = true + + getCurrentUserForLogin() + .then((user) => { + login(user, user.verifiedRoles ?? ["consumer"]) + }) + .catch(() => { + // no valid session — stay logged out + }) + }, [login, isAuthenticated]) + + return null +} + export function Providers({ children }: { children: React.ReactNode }) { const [queryClient] = useState( () => @@ -26,6 +49,7 @@ export function Providers({ children }: { children: React.ReactNode }) { + {children}