mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
consolidate callback hooks
This commit is contained in:
@@ -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]);
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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 <div>{code}</div>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user