some proper auth

This commit is contained in:
2025-08-15 15:27:24 -05:00
parent 4419dd7acc
commit 1c915d1713
9 changed files with 117 additions and 49 deletions

View File

@@ -17,8 +17,8 @@ model Game {
}
model Human {
key String @id
name String
key String @id @default(cuid(2))
name String @default("")
Instance Instance[]
}

View File

@@ -4,19 +4,33 @@ import { Prisma } from "@prisma/client";
import { simpleApi } from "./games/simple";
const api = new Elysia({ prefix: "/api" })
// [wip]
.group("/prisma", (app) =>
app
.post("/game", ({ body }: { body: Prisma.GameFindManyArgs }) =>
prisma.game.findMany(body)
)
.post(
"/instance",
({ body }: { body: Prisma.InstanceFindManyArgs }) =>
prisma.instance.findMany(body)
)
.onBeforeHandle(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() }) })
.post(
"/setName",
({ cookie: { token: humanKey }, body: { name } }) =>
prisma.human.update({
where: {
key: humanKey.value,
},
data: {
name,
},
}),
{
body: t.Object({
name: t.String(),
}),
}
)
.get("/games", () => prisma.game.findMany())
.get("/instances", ({ query: { game } }) =>
@@ -31,6 +45,7 @@ const api = new Elysia({ prefix: "/api" })
},
})
)
.use(simpleApi);
export default api;

View File

@@ -111,24 +111,21 @@ export const resolveAction = (
};
export const simpleApi = new Elysia({ prefix: "/simple" })
// .guard({ headers: t.Object({ Human: t.String() }) })
.post(
"/newGame",
(args: { body: { players: string[] }; headers: { human: string } }) => {
return prisma.instance.create({
data: {
gameState: newGame(args.body.players),
gameKey: "simple",
createdByKey: args.headers.human,
},
});
}
)
.guard({ cookie: t.Object({ token: t.String() }) })
.post("/newGame", ({ cookie: { token: humanKey } }) => {
return prisma.instance.create({
data: {
gameState: newGame([humanKey.value]),
gameKey: "simple",
createdByKey: humanKey.value,
},
});
})
.group("/:instanceId", (app) =>
app
.get(
"/",
({ params: { instanceId }, headers: { human: humanId } }) =>
({ params: { instanceId }, cookie: { token: humanKey } }) =>
prisma.instance
.findUnique({
where: {
@@ -139,9 +136,9 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
getView(
getKnowledge(
game!.gameState as GameState,
humanId!
humanKey.value
),
humanId!
humanKey.value
)
)
)
@@ -150,7 +147,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
({
params: { instanceId },
body: { action },
headers: { human: humanId },
cookie: { token: humanKey },
}) =>
prisma.instance
.findUniqueOrThrow({
@@ -161,7 +158,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
.then(async (game) => {
const newState = resolveAction(
game.gameState as GameState,
humanId!,
humanKey.value,
action
);
await prisma.instance.update({
@@ -171,8 +168,8 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
},
});
return getView(
getKnowledge(newState, humanId!),
humanId!
getKnowledge(newState, humanKey.value),
humanKey.value
);
}),
{

View File

@@ -6,7 +6,12 @@ import { staticPlugin } from "@elysiajs/static";
const port = env.PORT || 5001;
const app = new Elysia()
.use(cors())
.use(
cors({
origin: "http://localhost:3000",
credentials: true,
})
)
.onRequest(({ request }) => {
console.log(request.method, request.url);
})