From 305ead0918213b566c0d3e55db7b11905ec12648 Mon Sep 17 00:00:00 2001 From: xWafl <73sampleperson@gmail.com> Date: Tue, 14 Jul 2020 15:06:37 -0400 Subject: [PATCH] feat: admin only all users route --- packages/api/src/modules/users/actions/getAllUsers.ts | 11 +++++++++++ packages/api/src/modules/users/router.ts | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/api/src/modules/users/actions/getAllUsers.ts diff --git a/packages/api/src/modules/users/actions/getAllUsers.ts b/packages/api/src/modules/users/actions/getAllUsers.ts new file mode 100644 index 0000000..2c6661f --- /dev/null +++ b/packages/api/src/modules/users/actions/getAllUsers.ts @@ -0,0 +1,11 @@ +import User from "../types/User"; +import knex from "../../../../db/knex"; + +export default async () => { + const users = await knex("users"); + return users.map(l => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { password, ...data } = l; + return data; + }); +}; diff --git a/packages/api/src/modules/users/router.ts b/packages/api/src/modules/users/router.ts index d1e6e6f..b4ffb1a 100644 --- a/packages/api/src/modules/users/router.ts +++ b/packages/api/src/modules/users/router.ts @@ -13,9 +13,17 @@ import { registerBody } from "./schema/registerBody"; import { UpdateCountry } from "./schema/updateCountry"; import { RegisterBody } from "./types/RegisterBody"; import changePassword from "./actions/changePassword"; +import { requireAdmin } from "../auth/middleware/requireAdmin"; +import getAllUsers from "./actions/getAllUsers"; const router = new Router({ prefix: "/users" }); +router.get("/", requireAdmin(), async (ctx, next) => { + ctx.status = 200; + ctx.body = await getAllUsers(); + await next(); +}); + router.post( "/createUser", validateSchema(registerBody, "body"),