mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
useAuth Integration
This commit is contained in:
@@ -3,35 +3,18 @@
|
|||||||
import DashboardPage from "./DashboardPage";
|
import DashboardPage from "./DashboardPage";
|
||||||
import Loading from "@/components/Loading";
|
import Loading from "@/components/Loading";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import useAuth from "@/hooks/useAuth";
|
||||||
import axios from "axios";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const router = useRouter();
|
const { loading, username, name, validate } = useAuth();
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [username, setUsername] = useState("");
|
|
||||||
const [name, setName] = useState("");
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const init = async () => {
|
const runValidation = async () => {
|
||||||
const token = Cookies.get("token");
|
await validate();
|
||||||
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();
|
runValidation();
|
||||||
}, []);
|
}, [validate]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
|
|||||||
@@ -3,37 +3,18 @@
|
|||||||
import SettingsPage from "./SettingsPage";
|
import SettingsPage from "./SettingsPage";
|
||||||
import Loading from "@/components/Loading";
|
import Loading from "@/components/Loading";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import useAuth from "@/hooks/useAuth";
|
||||||
import axios from "axios";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const router = useRouter();
|
const { loading, username, name, email, validate } = useAuth();
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [username, setUsername] = useState("");
|
|
||||||
const [name, setName] = useState("");
|
|
||||||
const [email, setEmail] = useState("");
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const init = async () => {
|
const runValidation = async () => {
|
||||||
const token = Cookies.get("token");
|
await validate();
|
||||||
if (!token) {
|
};
|
||||||
router.push("/");
|
runValidation();
|
||||||
}
|
}, [validate]);
|
||||||
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();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ export default function Dashboard() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const runValidation = async () => {
|
const runValidation = async () => {
|
||||||
await validate();
|
await validate();
|
||||||
};
|
};
|
||||||
|
|
||||||
runValidation();
|
runValidation();
|
||||||
}, [validate]);
|
}, [validate]);
|
||||||
|
|||||||
@@ -3,35 +3,19 @@
|
|||||||
import SitesPage from "./SitesPage";
|
import SitesPage from "./SitesPage";
|
||||||
import Loading from "@/components/Loading";
|
import Loading from "@/components/Loading";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import useAuth from "@/hooks/useAuth";
|
||||||
import axios from "axios";
|
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const router = useRouter();
|
const { loading, username, name, validate } = useAuth();
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [username, setUsername] = useState("");
|
|
||||||
const [name, setName] = useState("");
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const init = async () => {
|
const runValidation = async () => {
|
||||||
const token = Cookies.get("token");
|
await validate();
|
||||||
if (!token) {
|
};
|
||||||
router.push("/");
|
|
||||||
}
|
runValidation();
|
||||||
const response = await axios.post("/api/user/validate", { token });
|
}, [validate]);
|
||||||
if (response.data.message !== "Valid") {
|
|
||||||
Cookies.remove("token");
|
|
||||||
router.push("/");
|
|
||||||
} else {
|
|
||||||
setUsername(response.data.username);
|
|
||||||
setName(response.data.name);
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
init();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const useAuth = () => {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const validate = useCallback(async () => {
|
const validate = useCallback(async () => {
|
||||||
@@ -18,25 +19,28 @@ const useAuth = () => {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
setValidated(false);
|
setValidated(false);
|
||||||
router.push("/");
|
router.push("/");
|
||||||
return;
|
return "Invalid";
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post("/api/user/validate", { token });
|
const response = await axios.post("/api/user/validate", { token });
|
||||||
|
|
||||||
if (response.data.message === "Valid") {
|
if (response.data.message === "Valid") {
|
||||||
setValidated(true);
|
setValidated(true);
|
||||||
setUsername(response.data.username);
|
setUsername(response.data.username);
|
||||||
setName(response.data.name);
|
setName(response.data.name);
|
||||||
|
setEmail(response.data.email);
|
||||||
|
return "Validated";
|
||||||
} else {
|
} else {
|
||||||
setValidated(false);
|
setValidated(false);
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
router.push("/");
|
router.push("/");
|
||||||
|
return "Invalid";
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Validation error:", error);
|
console.error("Validation error:", error);
|
||||||
setValidated(false);
|
setValidated(false);
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
router.push("/");
|
router.push("/");
|
||||||
|
return "Invalid";
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
@@ -51,9 +55,9 @@ const useAuth = () => {
|
|||||||
loading,
|
loading,
|
||||||
username,
|
username,
|
||||||
name,
|
name,
|
||||||
|
email,
|
||||||
validate
|
validate
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default useAuth;
|
export default useAuth;
|
||||||
Reference in New Issue
Block a user