consolidate callback hooks

This commit is contained in:
2026-06-08 12:50:09 +02:00
parent 430ec2db75
commit 3b29c42b3b
3 changed files with 21 additions and 30 deletions
-26
View File
@@ -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]);
};
+18 -2
View File
@@ -1,4 +1,6 @@
import { useNavigate } from "@tanstack/react-router";
import { useServerFn } from "@tanstack/react-start"; import { useServerFn } from "@tanstack/react-start";
import { useEffect } from "react";
import { useLichessUser } from "#/context/LichessUserContext.tsx"; import { useLichessUser } from "#/context/LichessUserContext.tsx";
import { import {
getSession, getSession,
@@ -47,13 +49,11 @@ export const useLichessOAuth = () => {
}; };
const lichessLogin = async () => { const lichessLogin = async () => {
// TODO add try/catch with toast
await loginFn(); await loginFn();
}; };
const lichessLogout = async () => { const lichessLogout = async () => {
console.log("Lichess Logout"); console.log("Lichess Logout");
// TODO remove cookies
await logoutFn(); await logoutFn();
setUser(null); setUser(null);
}; };
@@ -62,6 +62,21 @@ export const useLichessOAuth = () => {
return getSessionFn(); 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 { return {
lichessLogin, lichessLogin,
lichessLogout, lichessLogout,
@@ -69,5 +84,6 @@ export const useLichessOAuth = () => {
getLichessUser, getLichessUser,
setLichessUser, setLichessUser,
getLichessSession, getLichessSession,
useLichessCallback,
}; };
}; };
+3 -2
View File
@@ -1,11 +1,12 @@
import { getRouteApi } from "@tanstack/react-router"; import { getRouteApi } from "@tanstack/react-router";
import { useLichessCallback } from "#/hooks/useLichessCallback.ts"; import { useLichessOAuth } from "#/hooks/useLichessOAuth.ts";
const routeApi = getRouteApi("/callback"); const routeApi = getRouteApi("/callback");
const Callback = () => { const Callback = () => {
const { useLichessCallback } = useLichessOAuth();
const { code } = routeApi.useSearch(); const { code } = routeApi.useSearch();
useLichessCallback({ code }); useLichessCallback({ code }).then();
return <div>{code}</div>; return <div>{code}</div>;
}; };