Skip to content

Commit ed7ac4d

Browse files
Auth guard
1 parent ebde39b commit ed7ac4d

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

apps/frontend/src/app/page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
SearchOutlined,
2626
} from "@ant-design/icons";
2727
import "./styles.scss";
28-
import { useEffect, useState } from "react";
28+
import { useEffect, useState, useLayoutEffect } from "react";
2929
import {
3030
DeleteQuestion as DeleteQuestionByDocref,
3131
GetQuestions,
@@ -42,6 +42,8 @@ import {
4242
import Link from "next/link";
4343
import TextArea from "antd/es/input/TextArea";
4444
import { title } from "process";
45+
import { isAuthenticated } from "@/utils/Auth";
46+
import { redirect } from 'next/navigation';
4547

4648
/**
4749
* defines the State of the page whe a user is deleing an object. Has 3 general states:
@@ -117,6 +119,13 @@ export default function Home() {
117119
// State for refreshing data
118120
const [refresh, setRefresh] = useState(false);
119121

122+
useLayoutEffect(() => {
123+
const isAuth = isAuthenticated;
124+
if(!isAuth){
125+
redirect("/login")
126+
}
127+
}, [])
128+
120129
const handleEditClick = (index: number, question: Question) => {
121130
// Open the modal for the specific question
122131
const updatedModals =

apps/frontend/src/app/question/[id]/page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import {
2525
CheckCircleOutlined,
2626
} from "@ant-design/icons";
2727
import "./styles.scss";
28-
import { useEffect, useState } from "react";
28+
import { useEffect, useState, useLayoutEffect } from "react";
2929
import { Question, GetSingleQuestion } from "../../services/question";
3030
import React from "react";
3131
import TextArea from "antd/es/input/TextArea";
3232
import { useSearchParams } from "next/navigation";
3333
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions";
34+
import { isAuthenticated } from "@/utils/Auth";
35+
import { redirect } from 'next/navigation';
3436

3537
export default function Home() {
3638
const [isLoading, setIsLoading] = useState<boolean>(true); // Store the states related to table's loading
@@ -58,6 +60,13 @@ export default function Home() {
5860
const [description, setDescription] = useState<string | undefined>(undefined);
5961
const [selectedItem, setSelectedItem] = useState("python"); // State to hold the selected language item
6062

63+
useLayoutEffect(() => {
64+
const isAuth = isAuthenticated;
65+
if(!isAuth){
66+
redirect("/login")
67+
}
68+
}, [])
69+
6170
// When code editor page is initialised, fetch the particular question, and display in code editor
6271
useEffect(() => {
6372
if (!isLoading) {

apps/frontend/src/utils/Auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isAuthenticated = true;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# please add all private keys here
2-
2+
cs3219-g24-staging-firebase-adminsdk-suafv-9c0d1b2299.json
33
cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json
44

0 commit comments

Comments
 (0)