Improve accounts api and auth

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