mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-29 16:14:43 +00:00
useAuth
This commit is contained in:
@@ -3,37 +3,21 @@
|
||||
import SitePage from "./SitePage";
|
||||
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 { useParams } from "next/navigation";
|
||||
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 { siteId } = useParams();
|
||||
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 <Loading />;
|
||||
|
||||
Reference in New Issue
Block a user