diff --git a/app/dashboard/page.tsx b/app/dashboard/page.tsx
index 7842aaa..7d0f1d1 100644
--- a/app/dashboard/page.tsx
+++ b/app/dashboard/page.tsx
@@ -3,35 +3,18 @@
import DashboardPage from "./DashboardPage";
import Loading from "@/components/Loading";
-import { useState, useEffect } from "react";
-import { useRouter } from "next/navigation";
-import axios from "axios";
-import Cookies from "js-cookie";
+import { useEffect } from "react";
+import useAuth from "@/hooks/useAuth";
export default function Dashboard() {
- const router = useRouter();
- const [loading, setLoading] = useState(true);
- const [username, setUsername] = useState("");
- const [name, setName] = useState("");
+ const { loading, username, name, validate } = useAuth();
useEffect(() => {
- const init = async () => {
- const token = Cookies.get("token");
- if (!token) {
- router.push("/");
- }
- const response = await axios.post("/api/user/validate", { token });
- if (response.data.message !== "Valid") {
- Cookies.remove("token");
- router.push("/");
- } else {
- setUsername(response.data.username);
- setName(response.data.name);
- setLoading(false);
- }
+ const runValidation = async () => {
+ await validate();
};
- init();
- }, []);
+ runValidation();
+ }, [validate]);
if (loading) {
return ;
diff --git a/app/dashboard/settings/page.tsx b/app/dashboard/settings/page.tsx
index 794771f..57ccb5f 100644
--- a/app/dashboard/settings/page.tsx
+++ b/app/dashboard/settings/page.tsx
@@ -3,37 +3,18 @@
import SettingsPage from "./SettingsPage";
import Loading from "@/components/Loading";
-import { useState, useEffect } from "react";
-import { useRouter } from "next/navigation";
-import axios from "axios";
-import Cookies from "js-cookie";
+import { useEffect } from "react";
+import useAuth from "@/hooks/useAuth";
export default function Dashboard() {
- const router = useRouter();
- const [loading, setLoading] = useState(true);
- const [username, setUsername] = useState("");
- const [name, setName] = useState("");
- const [email, setEmail] = useState("");
+ const { loading, username, name, email, validate } = useAuth();
useEffect(() => {
- const init = async () => {
- const token = Cookies.get("token");
- if (!token) {
- router.push("/");
- }
- const response = await axios.post("/api/user/validate", { token });
- if (response.data.message !== "Valid") {
- Cookies.remove("token");
- router.push("/");
- } else {
- setUsername(response.data.username);
- setName(response.data.name);
- setEmail(response.data.email);
- setLoading(false);
- }
- };
- init();
- }, []);
+ const runValidation = async () => {
+ await validate();
+ };
+ runValidation();
+ }, [validate]);
if (loading) {
return ;
diff --git a/app/dashboard/sites/[siteId]/page.tsx b/app/dashboard/sites/[siteId]/page.tsx
index d10d4ad..97fef23 100644
--- a/app/dashboard/sites/[siteId]/page.tsx
+++ b/app/dashboard/sites/[siteId]/page.tsx
@@ -13,8 +13,8 @@ export default function Dashboard() {
useEffect(() => {
const runValidation = async () => {
- await validate();
- };
+ await validate();
+ };
runValidation();
}, [validate]);
diff --git a/app/dashboard/sites/page.tsx b/app/dashboard/sites/page.tsx
index b43cd8b..23bf158 100644
--- a/app/dashboard/sites/page.tsx
+++ b/app/dashboard/sites/page.tsx
@@ -3,35 +3,19 @@
import SitesPage from "./SitesPage";
import Loading from "@/components/Loading";
-import { useState, useEffect } from "react";
-import { useRouter } from "next/navigation";
-import axios from "axios";
-import Cookies from "js-cookie";
+import { useEffect } from "react";
+import useAuth from "@/hooks/useAuth";
export default function Dashboard() {
- const router = useRouter();
- const [loading, setLoading] = useState(true);
- const [username, setUsername] = useState("");
- const [name, setName] = useState("");
+ const { loading, username, name, validate } = useAuth();
useEffect(() => {
- const init = async () => {
- const token = Cookies.get("token");
- if (!token) {
- router.push("/");
- }
- const response = await axios.post("/api/user/validate", { token });
- if (response.data.message !== "Valid") {
- Cookies.remove("token");
- router.push("/");
- } else {
- setUsername(response.data.username);
- setName(response.data.name);
- setLoading(false);
- }
- };
- init();
- }, []);
+ const runValidation = async () => {
+ await validate();
+ };
+
+ runValidation();
+ }, [validate]);
if (loading) {
return ;
diff --git a/hooks/useAuth.ts b/hooks/useAuth.ts
index b2967bd..d617384 100644
--- a/hooks/useAuth.ts
+++ b/hooks/useAuth.ts
@@ -8,6 +8,7 @@ const useAuth = () => {
const [loading, setLoading] = useState(true);
const [username, setUsername] = useState("");
const [name, setName] = useState("");
+ const [email, setEmail] = useState("");
const router = useRouter();
const validate = useCallback(async () => {
@@ -18,25 +19,28 @@ const useAuth = () => {
setLoading(false);
setValidated(false);
router.push("/");
- return;
+ return "Invalid";
}
const response = await axios.post("/api/user/validate", { token });
-
if (response.data.message === "Valid") {
setValidated(true);
setUsername(response.data.username);
setName(response.data.name);
+ setEmail(response.data.email);
+ return "Validated";
} else {
setValidated(false);
Cookies.remove("token");
router.push("/");
+ return "Invalid";
}
} catch (error) {
console.error("Validation error:", error);
setValidated(false);
Cookies.remove("token");
router.push("/");
+ return "Invalid";
} finally {
setLoading(false);
}
@@ -51,9 +55,9 @@ const useAuth = () => {
loading,
username,
name,
+ email,
validate
};
}
-
export default useAuth;
\ No newline at end of file