Improve accounts api and auth
This commit is contained in:
+8
-10
@@ -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,6 @@ export const app = new Elysia()
|
|||||||
|
|
||||||
// ACCOUNTS
|
// ACCOUNTS
|
||||||
.group("/accounts", (app) => app
|
.group("/accounts", (app) => app
|
||||||
// Non-authenticated
|
|
||||||
.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 +457,12 @@ export const app = new Elysia()
|
|||||||
}, {
|
}, {
|
||||||
params: t.Object({ id: t.String() })
|
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 })
|
.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);
|
||||||
|
|||||||
Reference in New Issue
Block a user