diff --git a/web/components/auth-context.tsx b/web/components/auth-context.tsx index 19ced0b2..b3f4f2f8 100644 --- a/web/components/auth-context.tsx +++ b/web/components/auth-context.tsx @@ -58,14 +58,9 @@ export function AuthProvider(props: { serverUser?: AuthUser }) { const { children, serverUser } = props - const [authUser, setAuthUser] = useStateCheckEquality(serverUser) - - useEffect(() => { - if (serverUser === undefined) { - const cachedUser = localStorage.getItem(CACHED_USER_KEY) - setAuthUser(cachedUser && JSON.parse(cachedUser)) - } - }, [setAuthUser, serverUser]) + const [authUser, setAuthUser] = useStateCheckEquality( + serverUser ?? getStoredUser() + ) useEffect(() => { if (authUser != null) { @@ -134,3 +129,10 @@ export function AuthProvider(props: { {children} ) } + +const getStoredUser = () => { + if (typeof window === 'undefined') return undefined + + const json = localStorage.getItem(CACHED_USER_KEY) + return json ? JSON.parse(json) : undefined +}