From 57c153de0b364e02f35857267bae3d7c8a1730e9 Mon Sep 17 00:00:00 2001 From: potts99 Date: Mon, 4 Dec 2023 00:03:53 +0000 Subject: [PATCH] update user role fix --- apps/api/src/controllers/auth.ts | 21 +++++ .../UpdateUserModal/{index.js => index.tsx} | 86 ++++--------------- 2 files changed, 37 insertions(+), 70 deletions(-) rename apps/client/components/UpdateUserModal/{index.js => index.tsx} (66%) diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index d19bde81c..7f85004e1 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -579,4 +579,25 @@ export function authRoutes(fastify: FastifyInstance) { } } ); + + // Update a users role + fastify.put( + "/api/v1/auth/user/role", + async (request: FastifyRequest, reply: FastifyReply) => { + const bearer = request.headers.authorization!.split(" ")[1]; + const token = checkToken(bearer); + if (token) { + const { id, role } = request.body as { id: string; role: boolean }; + + await prisma.user.update({ + where: { id }, + data: { + isAdmin: role, + }, + }); + + reply.send({ success: true }); + } + } + ); } diff --git a/apps/client/components/UpdateUserModal/index.js b/apps/client/components/UpdateUserModal/index.tsx similarity index 66% rename from apps/client/components/UpdateUserModal/index.js rename to apps/client/components/UpdateUserModal/index.tsx index baf67ff17..d8f62daec 100644 --- a/apps/client/components/UpdateUserModal/index.js +++ b/apps/client/components/UpdateUserModal/index.tsx @@ -1,39 +1,29 @@ import { Dialog, Transition } from "@headlessui/react"; import { XMarkIcon } from "@heroicons/react/24/outline"; +import { getCookie } from "cookies-next"; import { useRouter } from "next/router"; -import React, { Fragment, useState } from "react"; +import { Fragment, useState } from "react"; export default function UpdateUserModal({ user }) { const [open, setOpen] = useState(false); - const [name, setName] = useState(user.name); - const [email, setEmail] = useState(user.email); const [admin, setAdmin] = useState(user.isAdmin); - const [error, setError] = useState(null); const router = useRouter(); - const notificationMethods = [ - { id: "user", title: "user" }, - { id: "admin", title: "admin" }, - ]; - async function updateUser() { - if (name.length > 0 && email.length > 0) { - await fetch("/api/v1/admin/user/update", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - email, - name, - admin, - id: user.id, - }), - }).then(() => router.reload(router.pathname)); - } - setError("Length needs to be more than zero"); + await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/user/role`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getCookie("session")}`, + }, + body: JSON.stringify({ + role: admin, + id: user.id, + }), + }); + // .then(() => router.reload()); } return ( @@ -43,7 +33,7 @@ export default function UpdateUserModal({ user }) { type="button" className="inline-flex items-center px-4 py-1.5 border font-semibold border-gray-300 shadow-sm text-xs rounded text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" > - Edit + Role @@ -97,54 +87,10 @@ export default function UpdateUserModal({ user }) { as="h3" className="text-lg leading-6 font-medium text-gray-900" > - Edit User + Edit User Role
- <> - setName(e.target.value)} - value={name} - focus={ - error !== null && name.length > 0 ? true : false - } - /> - {error !== null && name.length === 0 && ( -

{error}

- )} - - - <> - setEmail(e.target.value)} - value={email} - focus={ - error !== null && email.length > 0 ? true : false - } - /> - {error !== null && email.length === 0 && ( -

{error}

- )} - -
-