From 53bd2cacdc2b468100f7add679923c202e4b925a Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 2 Jul 2024 10:50:18 -0700 Subject: [PATCH 1/4] Added local state --- Client/src/App.jsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Client/src/App.jsx b/Client/src/App.jsx index 4a05c1de3..be667395a 100644 --- a/Client/src/App.jsx +++ b/Client/src/App.jsx @@ -19,8 +19,11 @@ import SetNewPassword from "./Pages/SetNewPassword"; import NewPasswordConfirmed from "./Pages/NewPasswordConfirmed"; import ToastComponent from "./Components/Toast"; import ProtectedRoute from "./Components/ProtectedRoute"; +import { useState, useEffect } from "react"; function App() { + const [adminExists, setAdminExists] = useState(false); // Assuming admin exists by default + return ( <> From cee88f63d9fef75c15786d283df86d88099227d6 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 2 Jul 2024 11:14:12 -0700 Subject: [PATCH 2/4] Make axios request --- Client/src/App.jsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Client/src/App.jsx b/Client/src/App.jsx index be667395a..193fbcb36 100644 --- a/Client/src/App.jsx +++ b/Client/src/App.jsx @@ -20,10 +20,26 @@ import NewPasswordConfirmed from "./Pages/NewPasswordConfirmed"; import ToastComponent from "./Components/Toast"; import ProtectedRoute from "./Components/ProtectedRoute"; import { useState, useEffect } from "react"; +import axios from "axios"; function App() { + const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; const [adminExists, setAdminExists] = useState(false); // Assuming admin exists by default + useEffect(() => { + const checkAdminExists = async () => { + try { + // Replace this URL with your actual API endpoint + const response = await axios.get(`${BASE_URL}/auth/users/admin`); + console.log(response); + } catch (error) { + console.error("Failed to check admin existence:", error); + } + }; + + checkAdminExists(); + }, []); + return ( <> From 299dadd24d8869c1f758662572bb14ffb6e589d0 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 2 Jul 2024 15:54:49 -0700 Subject: [PATCH 3/4] Check for admin, redirect to register/login as appropriate --- Client/src/App.jsx | 22 +--------------------- Client/src/Pages/Login/index.jsx | 15 +++++++++++++++ Client/src/Pages/Register/index.jsx | 9 +++++++++ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Client/src/App.jsx b/Client/src/App.jsx index 193fbcb36..26f75cef8 100644 --- a/Client/src/App.jsx +++ b/Client/src/App.jsx @@ -1,4 +1,4 @@ -import { Routes, Route } from "react-router-dom"; +import { Routes, Route, Navigate } from "react-router-dom"; // import "./App.css"; import NotFound from "./Pages/NotFound"; import Login from "./Pages/Login"; @@ -17,29 +17,9 @@ import ForgotPassword from "./Pages/ForgotPassword"; import CheckEmail from "./Pages/CheckEmail"; import SetNewPassword from "./Pages/SetNewPassword"; import NewPasswordConfirmed from "./Pages/NewPasswordConfirmed"; -import ToastComponent from "./Components/Toast"; import ProtectedRoute from "./Components/ProtectedRoute"; -import { useState, useEffect } from "react"; -import axios from "axios"; function App() { - const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; - const [adminExists, setAdminExists] = useState(false); // Assuming admin exists by default - - useEffect(() => { - const checkAdminExists = async () => { - try { - // Replace this URL with your actual API endpoint - const response = await axios.get(`${BASE_URL}/auth/users/admin`); - console.log(response); - } catch (error) { - console.error("Failed to check admin existence:", error); - } - }; - - checkAdminExists(); - }, []); - return ( <> diff --git a/Client/src/Pages/Login/index.jsx b/Client/src/Pages/Login/index.jsx index cebcaa39b..4beb4d2c8 100644 --- a/Client/src/Pages/Login/index.jsx +++ b/Client/src/Pages/Login/index.jsx @@ -13,6 +13,8 @@ import PasswordTextField from "../../Components/TextFields/Password/PasswordText import { loginValidation } from "../../Validation/validation"; import { login } from "../../Features/Auth/authSlice"; import { useDispatch } from "react-redux"; +import axios from "axios"; +const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; const Login = () => { const dispatch = useDispatch(); @@ -30,6 +32,19 @@ const Login = () => { password: "", }); + useEffect(() => { + axios + .get(BASE_URL + "/auth/users/admin") + .then((response) => { + if (response.data.data === false) { + navigate("/register"); + } + }) + .catch((error) => { + console.log(error); + }); + }, [navigate]); + useEffect(() => { const { error } = loginValidation.validate(form, { abortEarly: false, diff --git a/Client/src/Pages/Register/index.jsx b/Client/src/Pages/Register/index.jsx index 5d08e0c0d..709f46ca9 100644 --- a/Client/src/Pages/Register/index.jsx +++ b/Client/src/Pages/Register/index.jsx @@ -14,6 +14,7 @@ import { registerValidation } from "../../Validation/validation"; import { useDispatch } from "react-redux"; import { register } from "../../Features/Auth/authSlice"; +import axios from "axios"; const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; @@ -38,6 +39,14 @@ const Register = () => { password: "", }); + useEffect(() => { + axios.get(BASE_URL + "/auth/users/admin").then((response) => { + if (response.data.data === true) { + navigate("/login"); + } + }); + }); + useEffect(() => { const { error } = registerValidation.validate(form, { abortEarly: false, From 033101c8840f4fc78310426d2063c8a93a40cff4 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 2 Jul 2024 15:57:53 -0700 Subject: [PATCH 4/4] Added dependency array and catch block --- Client/src/Pages/Register/index.jsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Client/src/Pages/Register/index.jsx b/Client/src/Pages/Register/index.jsx index 709f46ca9..4aaf5afc8 100644 --- a/Client/src/Pages/Register/index.jsx +++ b/Client/src/Pages/Register/index.jsx @@ -40,12 +40,17 @@ const Register = () => { }); useEffect(() => { - axios.get(BASE_URL + "/auth/users/admin").then((response) => { - if (response.data.data === true) { - navigate("/login"); - } - }); - }); + axios + .get(BASE_URL + "/auth/users/admin") + .then((response) => { + if (response.data.data === true) { + navigate("/login"); + } + }) + .catch((error) => { + console.log(error); + }); + }, [navigate]); useEffect(() => { const { error } = registerValidation.validate(form, {