check for existing user on remount

This commit is contained in:
2026-06-08 12:40:52 +02:00
parent d1cdc7764b
commit 430ec2db75
4 changed files with 41 additions and 8 deletions
+19 -2
View File
@@ -1,12 +1,17 @@
import { useServerFn } from "@tanstack/react-start";
import {
createContext,
type Dispatch,
type ReactNode,
type SetStateAction,
useContext,
useEffect,
useState,
} from "react";
import { getSession } from "#/server/lichess.ts";
interface ILichessUser {
username: string;
id: string;
@@ -15,7 +20,7 @@ interface ILichessUser {
interface ILichessUserContextType {
user: ILichessUser | null;
setUser: Dispatch<SetStateAction<ILichessUser | null>>
setUser: Dispatch<SetStateAction<ILichessUser | null>>;
}
const LichessUserContext = createContext<ILichessUserContextType | undefined>(
@@ -24,8 +29,20 @@ const LichessUserContext = createContext<ILichessUserContextType | undefined>(
export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
const [user, setUser] = useState<ILichessUser | null>(null);
const getSessionFn = useServerFn(getSession);
// TODO useEffect to check and login user on mount
useEffect(() => {
(async () => {
const session = await getSessionFn();
if (session) {
setUser({
username: session.username,
id: session.id,
isLoggedIn: true,
});
}
})();
}, [getSessionFn]);
return (
<LichessUserContext.Provider value={{ user, setUser }}>