From dd572310f667e56b1945c268b0e73e5080d456bf Mon Sep 17 00:00:00 2001
From: Felipe D'Abrantes <felidabrantes@gmail>
Date: Sat, 22 Apr 2023 02:29:38 +0100
Subject: [PATCH] Redirect user from questions page if not an admin

---
 daily-thought-frontend/src/hooks/useUser.ts    |  4 ++--
 daily-thought-frontend/src/pages/profile.tsx   |  4 ++--
 daily-thought-frontend/src/pages/questions.tsx | 16 ++++++++++++++--
 daily-thought-frontend/src/types/user.ts       |  3 ++-
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/daily-thought-frontend/src/hooks/useUser.ts b/daily-thought-frontend/src/hooks/useUser.ts
index 78d3cf7a..489f757f 100644
--- a/daily-thought-frontend/src/hooks/useUser.ts
+++ b/daily-thought-frontend/src/hooks/useUser.ts
@@ -29,8 +29,8 @@ export const useUser = () => {
     if (!rehydrateUser) return;
 
     fetchUser().then((res) => {
-      const { _id, username, email, profile, firstName, lastName } = res;
-      setUser({ id: _id, email, username, profile, firstName, lastName });
+      const { _id, username, email, profile, firstName, lastName, admin } = res;
+      setUser({ id: _id, email, username, profile, firstName, lastName, admin });
       setRehydrateUser(false);
     });
   }, [rehydrateUser]);
diff --git a/daily-thought-frontend/src/pages/profile.tsx b/daily-thought-frontend/src/pages/profile.tsx
index 5bbf95db..bfee2b87 100644
--- a/daily-thought-frontend/src/pages/profile.tsx
+++ b/daily-thought-frontend/src/pages/profile.tsx
@@ -18,8 +18,8 @@ const Profile = () => {
   useEffect(() => {
     if (!user) {
       fetchUser().then((res) => {
-        const { _id, username, email, profile, firstName, lastName } = res;
-        setUser({ id: _id, email, username, profile, firstName, lastName });
+        const { _id, username, email, profile, firstName, lastName, admin } = res;
+        setUser({ id: _id, email, username, profile, firstName, lastName, admin });
       });
     }
   });
diff --git a/daily-thought-frontend/src/pages/questions.tsx b/daily-thought-frontend/src/pages/questions.tsx
index 57b6c06a..2e52500f 100644
--- a/daily-thought-frontend/src/pages/questions.tsx
+++ b/daily-thought-frontend/src/pages/questions.tsx
@@ -1,7 +1,8 @@
 import NavBar from '@/components/navigation/NavBar';
 import { useQuestions } from '@/hooks/useQuestions';
 import { useUser } from '@/hooks/useUser';
-import { useState } from 'react';
+import { useState, useEffect } from 'react';
+import Router from 'next/router';
 import NewQuestionForm from '@/components/questions/NewQuestionForm';
 import QuestionList from '@/components/questions/QuestionList';
 import { StatusMessage } from '@/types/statusMessage';
@@ -12,6 +13,13 @@ const Questions = () => {
   const [newQuestion, setNewQuestion] = useState('');
   const [statusMessage, setStatusMessage] = useState<StatusMessage | undefined>(undefined);
 
+  // Redirect user from page if not an admin
+  useEffect(() => {
+    if (user?.admin === false) {
+      Router.push('/feed');
+    }
+  }, [user]);
+
   const insertNewQuestion = async (newQuestion: String): Promise<void> => {
     const JSONdata = JSON.stringify({ questionText: newQuestion });
     const endpoint = `${process.env.NEXT_PUBLIC_FEED_SERVICE_URL}insertQuestion`;
@@ -89,7 +97,11 @@ const Questions = () => {
       {/* List of Questions */}
       <div className="flex flex-col items-center w-full shadow-lg h-full pb-4 overflow-auto">
         <h2 className="text-xl font-semibold leading-10 pt-4 text-gray-900">Questions Stored</h2>
-        {questions === undefined ? <p>Loading</p> : <QuestionList questions={questions} onDeleteClick={onQuestionDelete}/>}
+        {questions === undefined ? (
+          <p>Loading...</p>
+        ) : (
+          <QuestionList questions={questions} onDeleteClick={onQuestionDelete} />
+        )}
       </div>
     </div>
   );
diff --git a/daily-thought-frontend/src/types/user.ts b/daily-thought-frontend/src/types/user.ts
index 4a25fbc4..f20e500d 100644
--- a/daily-thought-frontend/src/types/user.ts
+++ b/daily-thought-frontend/src/types/user.ts
@@ -4,5 +4,6 @@ export type User = {
   username: string,
   id: string,
   firstName?: string,
-  lastName?: string
+  lastName?: string,
+  admin: boolean,
 }
\ No newline at end of file
-- 
GitLab