diff --git a/packages/client/src/app.tsx b/packages/client/src/app.tsx index d75eca2..3117a55 100644 --- a/packages/client/src/app.tsx +++ b/packages/client/src/app.tsx @@ -5,6 +5,7 @@ import "virtual:uno.css"; import pkg from "../package.json"; import "./style.css"; import api from "./api"; +import Cookies from "js-cookie"; const Profile = () => { let dialogRef!: HTMLDialogElement; @@ -59,4 +60,7 @@ const App = () => ( ); -render(App, document.getElementById("app")!); +// todo: fix this +(Cookies.get("token") == null ? api.whoami.post() : Promise.resolve()).then( + () => render(App, document.getElementById("app")!) +); diff --git a/packages/server/src/api.ts b/packages/server/src/api.ts index 6ccd679..6020b26 100644 --- a/packages/server/src/api.ts +++ b/packages/server/src/api.ts @@ -2,24 +2,24 @@ import { prisma } from "./db/db"; import { Elysia, t } from "elysia"; import { Prisma } from "@prisma/client"; import { simpleApi } from "./games/simple"; +import { human } from "./human"; const api = new Elysia({ prefix: "/api" }) - .onBeforeHandle(async ({ cookie: { token } }) => { + .post("/whoami", async ({ cookie: { token } }) => { if (token.value == null) { - console.log("CREATING NEW USER"); const newHuman = await prisma.human.create({ data: {}, }); token.value = newHuman.key; } }) - .guard({ cookie: t.Object({ token: t.String() }) }) + .use(human) .post( "/setName", - ({ cookie: { token: humanKey }, body: { name } }) => + ({ body: { name }, humanKey }) => prisma.human.update({ where: { - key: humanKey.value, + key: humanKey, }, data: { name, @@ -31,8 +31,8 @@ const api = new Elysia({ prefix: "/api" }) }), } ) - .get("/profile", ({ cookie: { token: humanKey } }) => - prisma.human.findFirst({ where: { key: humanKey.value } }) + .get("/profile", ({ humanKey }) => + prisma.human.findFirst({ where: { key: humanKey } }) ) .get("/games", () => prisma.game.findMany()) diff --git a/packages/server/src/games/simple.ts b/packages/server/src/games/simple.ts index ef855a0..e476e2f 100644 --- a/packages/server/src/games/simple.ts +++ b/packages/server/src/games/simple.ts @@ -9,6 +9,7 @@ import { import { heq } from "@games/shared/utils"; import { Elysia, t } from "elysia"; import { prisma } from "../db/db"; +import { human } from "../human"; // omniscient game state export type GameState = { @@ -111,44 +112,38 @@ export const resolveAction = ( }; export const simpleApi = new Elysia({ prefix: "/simple" }) - .guard({ cookie: t.Object({ token: t.String() }) }) - .post("/newGame", ({ cookie: { token: humanKey } }) => { + .use(human) + .post("/newGame", ({ humanKey }) => { return prisma.instance.create({ data: { - gameState: newGame([humanKey.value]), + gameState: newGame([humanKey]), gameKey: "simple", - createdByKey: humanKey.value, + createdByKey: humanKey, }, }); }) .group("/:instanceId", (app) => app - .get( - "/", - ({ params: { instanceId }, cookie: { token: humanKey } }) => - prisma.instance - .findUnique({ - where: { - id: instanceId, - }, - }) - .then((game) => - getView( - getKnowledge( - game!.gameState as GameState, - humanKey.value - ), - humanKey.value - ) + .get("/", ({ params: { instanceId }, humanKey }) => + prisma.instance + .findUnique({ + where: { + id: instanceId, + }, + }) + .then((game) => + getView( + getKnowledge( + game!.gameState as GameState, + humanKey + ), + humanKey ) + ) ) .post( "/", - ({ - params: { instanceId }, - body: { action }, - cookie: { token: humanKey }, - }) => + ({ params: { instanceId }, body: { action }, humanKey }) => prisma.instance .findUniqueOrThrow({ where: { @@ -158,7 +153,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" }) .then(async (game) => { const newState = resolveAction( game.gameState as GameState, - humanKey.value, + humanKey, action ); await prisma.instance.update({ @@ -168,8 +163,8 @@ export const simpleApi = new Elysia({ prefix: "/simple" }) }, }); return getView( - getKnowledge(newState, humanKey.value), - humanKey.value + getKnowledge(newState, humanKey), + humanKey ); }), { diff --git a/packages/server/src/human.ts b/packages/server/src/human.ts new file mode 100644 index 0000000..ad28947 --- /dev/null +++ b/packages/server/src/human.ts @@ -0,0 +1,8 @@ +import Elysia from "elysia"; + +export const human = new Elysia({ name: "human" }) + .derive(async ({ cookie: { token }, status }) => { + const humanKey = token.value; + return humanKey != null ? { humanKey } : status(401); + }) + .as("scoped"); diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index ff3b70a..ebdcfea 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -15,11 +15,6 @@ const app = new Elysia() .onRequest(({ request }) => { console.log(request.method, request.url); }) - .onError(({ code, error, body, headers }) => { - console.error("headers", JSON.stringify(headers, null, 2)); - console.error("body", JSON.stringify(body, null, 2)); - console.error(code, error); - }) .get("/ping", () => "pong") .use(api) .get("/*", () => Bun.file("./public/index.html"))