""auth"" working nicely
This commit is contained in:
@@ -5,6 +5,7 @@ import "virtual:uno.css";
|
|||||||
import pkg from "../package.json";
|
import pkg from "../package.json";
|
||||||
import "./style.css";
|
import "./style.css";
|
||||||
import api from "./api";
|
import api from "./api";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
const Profile = () => {
|
const Profile = () => {
|
||||||
let dialogRef!: HTMLDialogElement;
|
let dialogRef!: HTMLDialogElement;
|
||||||
@@ -59,4 +60,7 @@ const App = () => (
|
|||||||
</Router>
|
</Router>
|
||||||
);
|
);
|
||||||
|
|
||||||
render(App, document.getElementById("app")!);
|
// todo: fix this
|
||||||
|
(Cookies.get("token") == null ? api.whoami.post() : Promise.resolve()).then(
|
||||||
|
() => render(App, document.getElementById("app")!)
|
||||||
|
);
|
||||||
|
|||||||
@@ -2,24 +2,24 @@ import { prisma } from "./db/db";
|
|||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { simpleApi } from "./games/simple";
|
import { simpleApi } from "./games/simple";
|
||||||
|
import { human } from "./human";
|
||||||
|
|
||||||
const api = new Elysia({ prefix: "/api" })
|
const api = new Elysia({ prefix: "/api" })
|
||||||
.onBeforeHandle(async ({ cookie: { token } }) => {
|
.post("/whoami", async ({ cookie: { token } }) => {
|
||||||
if (token.value == null) {
|
if (token.value == null) {
|
||||||
console.log("CREATING NEW USER");
|
|
||||||
const newHuman = await prisma.human.create({
|
const newHuman = await prisma.human.create({
|
||||||
data: {},
|
data: {},
|
||||||
});
|
});
|
||||||
token.value = newHuman.key;
|
token.value = newHuman.key;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.guard({ cookie: t.Object({ token: t.String() }) })
|
.use(human)
|
||||||
.post(
|
.post(
|
||||||
"/setName",
|
"/setName",
|
||||||
({ cookie: { token: humanKey }, body: { name } }) =>
|
({ body: { name }, humanKey }) =>
|
||||||
prisma.human.update({
|
prisma.human.update({
|
||||||
where: {
|
where: {
|
||||||
key: humanKey.value,
|
key: humanKey,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
@@ -31,8 +31,8 @@ const api = new Elysia({ prefix: "/api" })
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.get("/profile", ({ cookie: { token: humanKey } }) =>
|
.get("/profile", ({ humanKey }) =>
|
||||||
prisma.human.findFirst({ where: { key: humanKey.value } })
|
prisma.human.findFirst({ where: { key: humanKey } })
|
||||||
)
|
)
|
||||||
.get("/games", () => prisma.game.findMany())
|
.get("/games", () => prisma.game.findMany())
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
import { heq } from "@games/shared/utils";
|
import { heq } from "@games/shared/utils";
|
||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { prisma } from "../db/db";
|
import { prisma } from "../db/db";
|
||||||
|
import { human } from "../human";
|
||||||
|
|
||||||
// omniscient game state
|
// omniscient game state
|
||||||
export type GameState = {
|
export type GameState = {
|
||||||
@@ -111,21 +112,19 @@ export const resolveAction = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const simpleApi = new Elysia({ prefix: "/simple" })
|
export const simpleApi = new Elysia({ prefix: "/simple" })
|
||||||
.guard({ cookie: t.Object({ token: t.String() }) })
|
.use(human)
|
||||||
.post("/newGame", ({ cookie: { token: humanKey } }) => {
|
.post("/newGame", ({ humanKey }) => {
|
||||||
return prisma.instance.create({
|
return prisma.instance.create({
|
||||||
data: {
|
data: {
|
||||||
gameState: newGame([humanKey.value]),
|
gameState: newGame([humanKey]),
|
||||||
gameKey: "simple",
|
gameKey: "simple",
|
||||||
createdByKey: humanKey.value,
|
createdByKey: humanKey,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.group("/:instanceId", (app) =>
|
.group("/:instanceId", (app) =>
|
||||||
app
|
app
|
||||||
.get(
|
.get("/", ({ params: { instanceId }, humanKey }) =>
|
||||||
"/",
|
|
||||||
({ params: { instanceId }, cookie: { token: humanKey } }) =>
|
|
||||||
prisma.instance
|
prisma.instance
|
||||||
.findUnique({
|
.findUnique({
|
||||||
where: {
|
where: {
|
||||||
@@ -136,19 +135,15 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
|
|||||||
getView(
|
getView(
|
||||||
getKnowledge(
|
getKnowledge(
|
||||||
game!.gameState as GameState,
|
game!.gameState as GameState,
|
||||||
humanKey.value
|
humanKey
|
||||||
),
|
),
|
||||||
humanKey.value
|
humanKey
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.post(
|
.post(
|
||||||
"/",
|
"/",
|
||||||
({
|
({ params: { instanceId }, body: { action }, humanKey }) =>
|
||||||
params: { instanceId },
|
|
||||||
body: { action },
|
|
||||||
cookie: { token: humanKey },
|
|
||||||
}) =>
|
|
||||||
prisma.instance
|
prisma.instance
|
||||||
.findUniqueOrThrow({
|
.findUniqueOrThrow({
|
||||||
where: {
|
where: {
|
||||||
@@ -158,7 +153,7 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
|
|||||||
.then(async (game) => {
|
.then(async (game) => {
|
||||||
const newState = resolveAction(
|
const newState = resolveAction(
|
||||||
game.gameState as GameState,
|
game.gameState as GameState,
|
||||||
humanKey.value,
|
humanKey,
|
||||||
action
|
action
|
||||||
);
|
);
|
||||||
await prisma.instance.update({
|
await prisma.instance.update({
|
||||||
@@ -168,8 +163,8 @@ export const simpleApi = new Elysia({ prefix: "/simple" })
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
return getView(
|
return getView(
|
||||||
getKnowledge(newState, humanKey.value),
|
getKnowledge(newState, humanKey),
|
||||||
humanKey.value
|
humanKey
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
|
|||||||
8
packages/server/src/human.ts
Normal file
8
packages/server/src/human.ts
Normal 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");
|
||||||
@@ -15,11 +15,6 @@ const app = new Elysia()
|
|||||||
.onRequest(({ request }) => {
|
.onRequest(({ request }) => {
|
||||||
console.log(request.method, request.url);
|
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")
|
.get("/ping", () => "pong")
|
||||||
.use(api)
|
.use(api)
|
||||||
.get("/*", () => Bun.file("./public/index.html"))
|
.get("/*", () => Bun.file("./public/index.html"))
|
||||||
|
|||||||
Reference in New Issue
Block a user