""auth"" working nicely

This commit is contained in:
2025-08-17 19:32:01 -04:00
parent c755b83d3d
commit ef5a5e059a
5 changed files with 44 additions and 42 deletions

View File

@@ -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 = () => (
</Router>
);
render(App, document.getElementById("app")!);
// todo: fix this
(Cookies.get("token") == null ? api.whoami.post() : Promise.resolve()).then(
() => render(App, document.getElementById("app")!)
);

View File

@@ -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())

View File

@@ -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,21 +112,19 @@ 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 } }) =>
.get("/", ({ params: { instanceId }, humanKey }) =>
prisma.instance
.findUnique({
where: {
@@ -136,19 +135,15 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
getView(
getKnowledge(
game!.gameState as GameState,
humanKey.value
humanKey
),
humanKey.value
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
);
}),
{

View File

@@ -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");

View File

@@ -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"))