diff --git a/daily-thought-frontend/src/hooks/useUser.ts b/daily-thought-frontend/src/hooks/useUser.ts index 78d3cf7ab47dcb936eabbd3ccb3d1cae202b9f5b..489f757f869f94e2d215473a20e677285f70eaff 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 5bbf95db078c06f6c1c4d18bf52cc556210c34fc..bfee2b87214e8691231843809516310034534058 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 57b6c06a9774e22faa38ae9e77f731a9858b66f2..2e52500f7b4596a242cf69173ddffdd0003cf144 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 4a25fbc4f122991e4b9d154e755474db368abc7b..f20e500d66a2e5542bc0d10d01ab7c90295c3f02 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