From 9d69d906d86683fa057e1a301f997502dd1e8d79 Mon Sep 17 00:00:00 2001 From: Guillaume Dorce Date: Fri, 14 Oct 2022 12:37:00 +0200 Subject: [PATCH] function to give admin right --- client/src/components/User.tsx | 19 +++++++++---- client/src/controllers/UserController.ts | 34 +++++++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/client/src/components/User.tsx b/client/src/components/User.tsx index faa8ce0..dcd3cb3 100644 --- a/client/src/components/User.tsx +++ b/client/src/components/User.tsx @@ -1,5 +1,7 @@ -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import Modal from './Modal'; +import { giveUserRights } from '@controllers/UserController'; +import { toastError, toastSuccess } from '@controllers/Toasts'; const User = ({ author }: any) => { const [show, setShow] = useState(false); @@ -29,9 +31,18 @@ const User = ({ author }: any) => { const handleRightClick = (e: any) => { e.preventDefault(); setMessageId(e.target.closest('.message').id.slice(9)); - setPopupPos({ posX: e.clientX, posY: e.clientY }); }; + + async function changeRights() { + setPopupPos({ posX: 0, posY: 0 }); + setMessageId('0'); + const response = await giveUserRights(author.id, 'ADMIN'); + if (response.error) { + return toastError(response.error); + } + toastSuccess('User rights changed'); + } return (
@@ -51,9 +62,7 @@ const User = ({ author }: any) => {
diff --git a/client/src/controllers/UserController.ts b/client/src/controllers/UserController.ts index 48b0df3..d97e76e 100644 --- a/client/src/controllers/UserController.ts +++ b/client/src/controllers/UserController.ts @@ -1,7 +1,8 @@ import { Cookies } from 'react-cookie'; +const token = new Cookies().get('token'); + const getMeInfo = async () => { - const token = new Cookies().get('token'); const response = await fetch('/api/me', { method: 'GET', mode: 'cors', @@ -13,7 +14,7 @@ const getMeInfo = async () => { return response.json(); }; -const login = async ({email, password}: {email: string, password: string}) => { +const login = async ({ email, password }: { email: string; password: string }) => { const response = await fetch('/api/auth/login', { method: 'POST', body: JSON.stringify({ email, password }), @@ -33,15 +34,15 @@ const signup = async (formData: FormData) => { const form = { email: formData.get('email') as string, password: formData.get('password') as string, - "password-confirm": formData.get('password-confirm') as string, + 'password-confirm': formData.get('password-confirm') as string, firstName: formData.get('firstName') as string, lastName: formData.get('lastName') as string, }; - - if (form.password !== form["password-confirm"]) { - throw "Passwords do not match"; + + if (form.password !== form['password-confirm']) { + throw 'Passwords do not match'; } - + const response = await fetch('/api/auth/signup', { method: 'POST', body: JSON.stringify(form), @@ -57,4 +58,23 @@ const signup = async (formData: FormData) => { return data; }; +export const giveUserRights = async (userId: string, right: string) => { + const response = await fetch(`/api/users/${userId}/rights`, { + method: 'POST', + mode: 'cors', + headers: { + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ right }), + }); + if (!response.ok) { + return {error: response.statusText}; + } + const data = await response.json(); + if (data.error) { + return {error: data.error}; + } + return data; +}; + export { getMeInfo, login, signup };