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 { 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,
};
};