Skip to content
Snippets Groups Projects
Commit dd572310 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Redirect user from questions page if not an admin

parent b26fc83b
No related branches found
No related tags found
1 merge request!25Add a question management page
...@@ -29,8 +29,8 @@ export const useUser = () => { ...@@ -29,8 +29,8 @@ export const useUser = () => {
if (!rehydrateUser) return; if (!rehydrateUser) return;
fetchUser().then((res) => { fetchUser().then((res) => {
const { _id, username, email, profile, firstName, lastName } = res; const { _id, username, email, profile, firstName, lastName, admin } = res;
setUser({ id: _id, email, username, profile, firstName, lastName }); setUser({ id: _id, email, username, profile, firstName, lastName, admin });
setRehydrateUser(false); setRehydrateUser(false);
}); });
}, [rehydrateUser]); }, [rehydrateUser]);
......
...@@ -18,8 +18,8 @@ const Profile = () => { ...@@ -18,8 +18,8 @@ const Profile = () => {
useEffect(() => { useEffect(() => {
if (!user) { if (!user) {
fetchUser().then((res) => { fetchUser().then((res) => {
const { _id, username, email, profile, firstName, lastName } = res; const { _id, username, email, profile, firstName, lastName, admin } = res;
setUser({ id: _id, email, username, profile, firstName, lastName }); setUser({ id: _id, email, username, profile, firstName, lastName, admin });
}); });
} }
}); });
......
import NavBar from '@/components/navigation/NavBar'; import NavBar from '@/components/navigation/NavBar';
import { useQuestions } from '@/hooks/useQuestions'; import { useQuestions } from '@/hooks/useQuestions';
import { useUser } from '@/hooks/useUser'; 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 NewQuestionForm from '@/components/questions/NewQuestionForm';
import QuestionList from '@/components/questions/QuestionList'; import QuestionList from '@/components/questions/QuestionList';
import { StatusMessage } from '@/types/statusMessage'; import { StatusMessage } from '@/types/statusMessage';
...@@ -12,6 +13,13 @@ const Questions = () => { ...@@ -12,6 +13,13 @@ const Questions = () => {
const [newQuestion, setNewQuestion] = useState(''); const [newQuestion, setNewQuestion] = useState('');
const [statusMessage, setStatusMessage] = useState<StatusMessage | undefined>(undefined); 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 insertNewQuestion = async (newQuestion: String): Promise<void> => {
const JSONdata = JSON.stringify({ questionText: newQuestion }); const JSONdata = JSON.stringify({ questionText: newQuestion });
const endpoint = `${process.env.NEXT_PUBLIC_FEED_SERVICE_URL}insertQuestion`; const endpoint = `${process.env.NEXT_PUBLIC_FEED_SERVICE_URL}insertQuestion`;
...@@ -89,7 +97,11 @@ const Questions = () => { ...@@ -89,7 +97,11 @@ const Questions = () => {
{/* List of Questions */} {/* List of Questions */}
<div className="flex flex-col items-center w-full shadow-lg h-full pb-4 overflow-auto"> <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> <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>
</div> </div>
); );
......
...@@ -4,5 +4,6 @@ export type User = { ...@@ -4,5 +4,6 @@ export type User = {
username: string, username: string,
id: string, id: string,
firstName?: string, firstName?: string,
lastName?: string lastName?: string,
admin: boolean,
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment