Improve accounts api and auth

This commit is contained in:
Dominic Ferrando
2026-08-16 02:11:30 -04:00
parent 17af2f3d1f
commit 9bb769fab0
+8 -10
View File
@@ -63,14 +63,8 @@ const authPlugin = new Elysia({ name: "auth" })
authUser: { authUser: {
async resolve({ jwt, cookie: { auth } }) { async resolve({ jwt, cookie: { auth } }) {
const token = auth.value && await jwt.verify(auth.value); const token = auth.value && await jwt.verify(auth.value);
if (!token || !token.sessionId || (token.role !== "user" && token.role !== "admin")) throw status(401, "Unauthorized"); if (!token || token.role !== "user" || !token.accountId || !token.sessionId) throw status(401, "Unauthorized");
if (token.role === "user" && !token.accountId) throw status(401, "Unauthorized"); return { sessionId: token.sessionId.toString(), accountId: token.accountId.toString() };
return {
sessionId: token.sessionId.toString(),
isAdmin: token.role === "admin",
accountId: token.role === "user" ? token.accountId!.toString() : null,
};
} }
} }
}); });
@@ -456,7 +450,12 @@ export const app = new Elysia()
// ACCOUNTS // ACCOUNTS
.group("/accounts", (app) => app .group("/accounts", (app) => app
// Non-authenticated .guard({ authUser: true })
.get("/me/stats", async ({ accountId }) => {
const stats = await s.Accounts.stats(accountId);
if (!stats) throw new NotFoundError("Account stats not found");
return stats;
})
.get("/:id/stats", async ({ params: { id } }) => { .get("/:id/stats", async ({ params: { id } }) => {
const stats = await s.Accounts.stats(id); const stats = await s.Accounts.stats(id);
if (!stats) throw new NotFoundError("Account stats not found"); if (!stats) throw new NotFoundError("Account stats not found");
@@ -464,7 +463,6 @@ export const app = new Elysia()
}, { }, {
params: t.Object({ id: t.String() }) params: t.Object({ id: t.String() })
}) })
// Authenticated
.guard({ authAdmin: true }) .guard({ authAdmin: true })
.get("/:id", async ({ params: { id } }) => { .get("/:id", async ({ params: { id } }) => {
const account = await s.Accounts.get(id); const account = await s.Accounts.get(id);