diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 3145d4880..118169152 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -136,6 +136,20 @@ export function authRoutes(fastify: FastifyInstance) { } ); + // Delete a user + fastify.delete( + "/api/v1/auth/user/:id", + async (request: FastifyRequest, reply: FastifyReply) => { + const { id } = request.params as { id: string }; + + await prisma.user.delete({ + where: { id }, + }); + + reply.send({ success: true }); + } + ); + // User Profile fastify.get( "/api/v1/auth/profile", diff --git a/apps/client/pages/admin/users/internal/index.js b/apps/client/pages/admin/users/internal/index.js index a2edc4763..537e1de66 100644 --- a/apps/client/pages/admin/users/internal/index.js +++ b/apps/client/pages/admin/users/internal/index.js @@ -205,23 +205,28 @@ export default function UserAuthPanel() { fetchUsers(token) ); - // async function deleteUser(client) { - // const id = client.id; - // try { - // await fetch(`/api/v1/auth/delete/${id}`, { - // method: "DELETE", - // headers: { - // "Content-Type": "application/json", - // }, - // }) - // .then((response) => response.json()) - // .then(() => { - // refetch; - // }); - // } catch (error) { - // console.log(error); - // } - // } + async function deleteUser(id) { + if (confirm("Are you sure you want to delete this user?")) { + try { + await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/user/${id}`, + { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + } + ) + .then((response) => response.json()) + .then(() => { + refetch; + }); + } catch (error) { + console.log(error); + } + } + } const columns = React.useMemo( () => [ @@ -239,22 +244,20 @@ export default function UserAuthPanel() { { Header: "", id: "actions", - Cell: ({ row, value }) => { + Cell: ({ row }) => { return (
- {/* deleteClient(row.cells[0].value)} - > - - */} + {row.original.isAdmin ? null : ( + + )}
); }, @@ -337,17 +340,6 @@ export default function UserAuthPanel() { refetch={() => handleRefresh} /> - {/* deleteClient(user.id)} - > - - */} ))}