diff --git a/client/src/components/AppHeader.tsx b/client/src/components/AppHeader.tsx index a3abe15..7c41be2 100644 --- a/client/src/components/AppHeader.tsx +++ b/client/src/components/AppHeader.tsx @@ -1,20 +1,8 @@ import logo from '@assets/images/logo.svg'; import { useQuery } from '@tanstack/react-query'; -import { Cookies, withCookies, useCookies } from 'react-cookie'; +import { useCookies } from 'react-cookie'; import Avatar from '@components/Avatar'; - -const getMeInfo = async () => { - const token = new Cookies().get('token'); - const response = await fetch('/api/me', { - method: 'GET', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}`, - }, - }); - return response.json(); -}; +import { getMeInfo } from '@controllers/UserController'; const AppHeader = () => { const meInfo = useQuery(['me'], getMeInfo, { @@ -58,4 +46,4 @@ const AppHeader = () => { ); }; -export default withCookies(AppHeader); +export default AppHeader; diff --git a/client/src/components/Message.tsx b/client/src/components/Message.tsx index 76e02f0..6e4919c 100644 --- a/client/src/components/Message.tsx +++ b/client/src/components/Message.tsx @@ -1,6 +1,9 @@ import { FaThumbsUp } from 'react-icons/fa'; import Avatar from '@components/Avatar'; import PopupMenu from './PopupMenu'; +import { getMeInfo } from '@controllers/UserController'; +import { useQuery } from '@tanstack/react-query'; +import { toastError } from '@controllers/Toasts'; const Image = ({ image }: { image: string }) => { if (image === '' || image === null) { @@ -30,6 +33,15 @@ const Likes = ({ likes }: { likes: number }) => { }; const Message = ({ message }: any) => { + const me = useQuery(['me'], getMeInfo, { + onSuccess: (data) => { + return data; + }, + onError: (error) => { + toastError(error as string); + }, + }); + return ( <>
{
{message.author.firstName} {message.author.lastName}
- + {(me.data?.id === message.author.id) || (me.data?.role === 'ADMIN') ? ( + + ) : null}
diff --git a/client/src/controllers/UserController.ts b/client/src/controllers/UserController.ts new file mode 100644 index 0000000..d49281c --- /dev/null +++ b/client/src/controllers/UserController.ts @@ -0,0 +1,48 @@ +import { Cookies } from 'react-cookie'; + +const getMeInfo = async () => { + const token = new Cookies().get('token'); + const response = await fetch('/api/me', { + method: 'GET', + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + }); + return response.json(); +}; + +const login = async (email: string, password: string) => { + const response = await fetch('/api/auth/login', { + method: 'POST', + body: JSON.stringify({ email, password }), + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + }, + }); + const data = await response.json(); + if (data.error) { + throw data.error; + } + return data; +}; + +const signup = async (email: string, password: string) => { + const response = await fetch('/api/auth/signup', { + method: 'POST', + body: JSON.stringify({ email, password }), + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + }, + }); + const data = await response.json(); + if (data.error) { + throw data.error; + } + return data; +}; + +export { getMeInfo, login, signup };