mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
check for existing user on remount
This commit is contained in:
@@ -3,7 +3,7 @@ import { useLichessOAuth } from "#/hooks/useLichessOAuth.ts";
|
|||||||
|
|
||||||
const LichessButton = () => {
|
const LichessButton = () => {
|
||||||
const { lichessLogin, lichessLogout } = useLichessOAuth();
|
const { lichessLogin, lichessLogout } = useLichessOAuth();
|
||||||
const { user, setUser, logout } = useLichessUser();
|
const { user } = useLichessUser();
|
||||||
|
|
||||||
const onClickHandler = () => {
|
const onClickHandler = () => {
|
||||||
return user?.isLoggedIn ? lichessLogout() : lichessLogin();
|
return user?.isLoggedIn ? lichessLogout() : lichessLogin();
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
|
import { useServerFn } from "@tanstack/react-start";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
type Dispatch,
|
type Dispatch,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
type SetStateAction,
|
type SetStateAction,
|
||||||
useContext,
|
useContext,
|
||||||
|
useEffect,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
|
||||||
|
import { getSession } from "#/server/lichess.ts";
|
||||||
|
|
||||||
interface ILichessUser {
|
interface ILichessUser {
|
||||||
username: string;
|
username: string;
|
||||||
id: string;
|
id: string;
|
||||||
@@ -15,7 +20,7 @@ interface ILichessUser {
|
|||||||
|
|
||||||
interface ILichessUserContextType {
|
interface ILichessUserContextType {
|
||||||
user: ILichessUser | null;
|
user: ILichessUser | null;
|
||||||
setUser: Dispatch<SetStateAction<ILichessUser | null>>
|
setUser: Dispatch<SetStateAction<ILichessUser | null>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const LichessUserContext = createContext<ILichessUserContextType | undefined>(
|
const LichessUserContext = createContext<ILichessUserContextType | undefined>(
|
||||||
@@ -24,8 +29,20 @@ const LichessUserContext = createContext<ILichessUserContextType | undefined>(
|
|||||||
|
|
||||||
export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
|
export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const [user, setUser] = useState<ILichessUser | null>(null);
|
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 (
|
return (
|
||||||
<LichessUserContext.Provider value={{ user, setUser }}>
|
<LichessUserContext.Provider value={{ user, setUser }}>
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import { useServerFn } from "@tanstack/react-start";
|
import { useServerFn } from "@tanstack/react-start";
|
||||||
import { useLichessUser } from "#/context/LichessUserContext.tsx";
|
import { useLichessUser } from "#/context/LichessUserContext.tsx";
|
||||||
import { getToken, getUser, login, setSession, logout } from "#/server/lichess.ts";
|
import {
|
||||||
|
getSession,
|
||||||
|
getToken,
|
||||||
|
getUser,
|
||||||
|
login,
|
||||||
|
logout,
|
||||||
|
setSession,
|
||||||
|
} from "#/server/lichess.ts";
|
||||||
|
|
||||||
interface ITokenData {
|
interface ITokenData {
|
||||||
access_token: string;
|
access_token: string;
|
||||||
@@ -12,10 +19,11 @@ export const useLichessOAuth = () => {
|
|||||||
const { setUser } = useLichessUser();
|
const { setUser } = useLichessUser();
|
||||||
|
|
||||||
const loginFn = useServerFn(login);
|
const loginFn = useServerFn(login);
|
||||||
const logoutFn = useServerFn(logout)
|
const logoutFn = useServerFn(logout);
|
||||||
const accessTokenFn = useServerFn(getToken);
|
const accessTokenFn = useServerFn(getToken);
|
||||||
const getUserFn = useServerFn(getUser);
|
const getUserFn = useServerFn(getUser);
|
||||||
const setSessionFn = useServerFn(setSession);
|
const setSessionFn = useServerFn(setSession);
|
||||||
|
const getSessionFn = useServerFn(getSession);
|
||||||
|
|
||||||
const lichessAccessToken = async ({ code }: { code: string }) => {
|
const lichessAccessToken = async ({ code }: { code: string }) => {
|
||||||
return accessTokenFn({ data: { code } });
|
return accessTokenFn({ data: { code } });
|
||||||
@@ -50,11 +58,16 @@ export const useLichessOAuth = () => {
|
|||||||
setUser(null);
|
setUser(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getLichessSession = async () => {
|
||||||
|
return getSessionFn();
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
lichessLogin,
|
lichessLogin,
|
||||||
lichessLogout,
|
lichessLogout,
|
||||||
lichessAccessToken,
|
lichessAccessToken,
|
||||||
getLichessUser,
|
getLichessUser,
|
||||||
setLichessUser,
|
setLichessUser,
|
||||||
|
getLichessSession,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { randomBytes } from "node:crypto";
|
import { randomBytes } from "node:crypto";
|
||||||
import { redirect } from "@tanstack/react-router";
|
import { redirect } from "@tanstack/react-router";
|
||||||
import { createServerFn } from "@tanstack/react-start";
|
import { createServerFn } from "@tanstack/react-start";
|
||||||
import { getCookie, setCookie, deleteCookie } from "@tanstack/react-start/server";
|
import {
|
||||||
|
deleteCookie,
|
||||||
|
getCookie,
|
||||||
|
setCookie,
|
||||||
|
} from "@tanstack/react-start/server";
|
||||||
import { env } from "#/env.ts";
|
import { env } from "#/env.ts";
|
||||||
import { base64UrlEncode, sha256 } from "#/lib/encodeDecode.ts";
|
import { base64UrlEncode, sha256 } from "#/lib/encodeDecode.ts";
|
||||||
|
|
||||||
@@ -13,13 +17,12 @@ const getRedirectUri = () =>
|
|||||||
? `https://${env.LICHESS_CLIENT_ID}/callback`
|
? `https://${env.LICHESS_CLIENT_ID}/callback`
|
||||||
: "http://localhost:3000/callback";
|
: "http://localhost:3000/callback";
|
||||||
|
|
||||||
// TODO fix and check
|
|
||||||
export const getSession = createServerFn({ method: "GET" }).handler(
|
export const getSession = createServerFn({ method: "GET" }).handler(
|
||||||
async () => {
|
async () => {
|
||||||
const session = getCookie("lichess-session");
|
const session = getCookie("lichess-session");
|
||||||
if (!session) return null;
|
if (!session) return null;
|
||||||
try {
|
try {
|
||||||
return JSON.parse(session) as { username: string };
|
return JSON.parse(session) as { username: string; id: string };
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user