Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lichess sessions authentication logic #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 71 additions & 53 deletions src/components/home/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,100 @@ import { useEffect, useRef, useState } from "react";
import AccountCards from "../common/AccountCards";
import GenericCard from "../common/GenericCard";
import LichessLogo from "./LichessLogo";
import type { ChessComSession, LichessSession } from "@/utils/session";

function Accounts() {
const [, setSessions] = useAtom(sessionsAtom);
const isListesning = useRef(false);
const isListening = useRef(false);
const [databases, setDatabases] = useState<DatabaseInfo[]>([]);
useEffect(() => {
getDatabases().then((dbs) => setDatabases(dbs));
}, []);
const [open, setOpen] = useState(false);

async function login(username: string) {
await commands.authenticate(username);
function addChessComSession(alias: string, session: ChessComSession) {
setSessions((sessions) => {
const newSessions = sessions.filter(
(s) => s.chessCom?.username !== session.username,
);
return [
...newSessions,
{
chessCom: session,
player: alias,
updatedAt: Date.now()
},
];
});
}

function addLichessSession(alias: string, session: LichessSession) {
setSessions((sessions) => {
const newSessions = sessions.filter(
(s) => s.lichess?.username !== session.username,
);
return [
...newSessions,
{
lichess: session,
player: alias,
updatedAt: Date.now()
},
];
});
}

async function addChessCom(
player: string,
username: string
) {
const p = player !== "" ? player : username;
const stats = await getChessComAccount(username);
if (!stats) {
return;
}
addChessComSession(p, { username, stats });
};

async function addLichessNoLogin(
player: string,
username: string
) {
const p = player !== "" ? player : username;
const account = await getLichessAccount({ username });
if (!account) return;
addLichessSession(p, { username, account });
}

async function onLichessAuthentication(token: string) {
const player = sessionStorage.getItem("lichess_player_alias") || "";
sessionStorage.removeItem("lichess_player_alias");
const account = await getLichessAccount({ token });
if (!account) return;
const username = account.username;
const p = player !== "" ? player : username;
addLichessSession(p, { accessToken: token, username: username, account });
}

async function addLichess(
player: string,
username: string,
withLogin: boolean,
) {
const p = player !== "" ? player : username;
if (withLogin) {
login(username);
} else {
const account = await getLichessAccount({
username,
});
if (!account) return;
setSessions((sessions) => {
const newSessions = sessions.filter(
(s) => s.lichess?.username !== username,
);
return [
...newSessions,
{ lichess: { username, account }, player: p, updatedAt: Date.now() },
];
});
sessionStorage.setItem("lichess_player_alias", player);
return await commands.authenticate(username);
}
return await addLichessNoLogin(player, username);
}

useEffect(() => {
async function listen_for_code() {
if (isListesning.current) return;
isListesning.current = true;
if (isListening.current) return;
isListening.current = true;
await listen<string>("access_token", async (event) => {
const token = event.payload;
const account = await getLichessAccount({ token });
if (!account) return;
setSessions((sessions) => [
...sessions,
{
lichess: {
accessToken: token,
account,
username: account.username,
},
updatedAt: Date.now(),
},
]);
await onLichessAuthentication(token);
});
}

Expand All @@ -100,25 +136,7 @@ function Accounts() {
open={open}
setOpen={setOpen}
addLichess={addLichess}
addChessCom={(player, username) => {
getChessComAccount(username).then((stats) => {
const p = player !== "" ? player : username;
if (!stats) return;
setSessions((sessions) => {
const newSessions = sessions.filter(
(s) => s.chessCom?.username !== username,
);
return [
...newSessions,
{
chessCom: { username, stats },
player: p,
updatedAt: Date.now(),
},
];
});
});
}}
addChessCom={addChessCom}
/>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/session.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { ChessComStats } from "@/utils/chess.com/api";
import type { LichessAccount } from "@/utils/lichess/api";

type LichessSession = {
export type LichessSession = {
accessToken?: string;
username: string;
account: LichessAccount;
};

type ChessComSession = {
export type ChessComSession = {
username: string;
stats: ChessComStats;
};
Expand Down