diff --git a/src/hooks/useLichessCallback.ts b/src/hooks/useLichessCallback.ts deleted file mode 100644 index 9fe598b..0000000 --- a/src/hooks/useLichessCallback.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { useNavigate } from "@tanstack/react-router"; -import { useEffect } from "react"; -import { useLichessOAuth } from "#/hooks/useLichessOAuth.ts"; - -interface IProps { - code: string; -} - -export const useLichessCallback = ({ code }: IProps) => { - const navigate = useNavigate(); - - const { lichessAccessToken, getLichessUser, setLichessUser } = - useLichessOAuth(); - - useEffect(() => { - if (!code) return; - - (async () => { - const tokenData = await lichessAccessToken({ code }); - const userData = await getLichessUser({ tokenData }); - await setLichessUser({ username: userData.username, id: userData.id }); - await navigate({ to: "/chessboard" }); - // TODO set logging in state then redirect - })(); - }, [code, lichessAccessToken, getLichessUser, setLichessUser, navigate]); -}; diff --git a/src/hooks/useLichessOAuth.ts b/src/hooks/useLichessOAuth.ts index 1b37e2a..1c07cf8 100644 --- a/src/hooks/useLichessOAuth.ts +++ b/src/hooks/useLichessOAuth.ts @@ -1,4 +1,6 @@ +import { useNavigate } from "@tanstack/react-router"; import { useServerFn } from "@tanstack/react-start"; +import { useEffect } from "react"; import { useLichessUser } from "#/context/LichessUserContext.tsx"; import { getSession, @@ -47,13 +49,11 @@ export const useLichessOAuth = () => { }; const lichessLogin = async () => { - // TODO add try/catch with toast await loginFn(); }; const lichessLogout = async () => { console.log("Lichess Logout"); - // TODO remove cookies await logoutFn(); setUser(null); }; @@ -62,6 +62,21 @@ export const useLichessOAuth = () => { return getSessionFn(); }; + const useLichessCallback = async ({ code }: { code: string }) => { + const navigate = useNavigate(); + + useEffect(() => { + if (!code) return; + + (async () => { + const tokenData = await lichessAccessToken({ code }); + const userData = await getLichessUser({ tokenData }); + await setLichessUser({ username: userData.username, id: userData.id }); + await navigate({ to: "/chessboard" }); + })(); + }, [navigate, code]); + }; + return { lichessLogin, lichessLogout, @@ -69,5 +84,6 @@ export const useLichessOAuth = () => { getLichessUser, setLichessUser, getLichessSession, + useLichessCallback, }; }; diff --git a/src/pages/Callback.tsx b/src/pages/Callback.tsx index 607abee..045dacb 100644 --- a/src/pages/Callback.tsx +++ b/src/pages/Callback.tsx @@ -1,11 +1,12 @@ import { getRouteApi } from "@tanstack/react-router"; -import { useLichessCallback } from "#/hooks/useLichessCallback.ts"; +import { useLichessOAuth } from "#/hooks/useLichessOAuth.ts"; const routeApi = getRouteApi("/callback"); const Callback = () => { + const { useLichessCallback } = useLichessOAuth(); const { code } = routeApi.useSearch(); - useLichessCallback({ code }); + useLichessCallback({ code }).then(); return
{code}
; };