mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
useAuth
This commit is contained in:
parent
59a32e0407
commit
2dea29287a
@ -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 />;
|
||||
|
||||
@ -14,12 +14,11 @@ export default function Login() {
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
const response = await axios.get("/api/user/init");
|
||||
if (response.data.message === "No users found") {
|
||||
await axios.get("/api/user/init").then(() => {
|
||||
router.push("/setup");
|
||||
} else {
|
||||
setLoading(false);
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
};
|
||||
init();
|
||||
|
||||
|
||||
59
hooks/useAuth.ts
Normal file
59
hooks/useAuth.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
const useAuth = () => {
|
||||
const [validated, setValidated] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [username, setUsername] = useState("");
|
||||
const [name, setName] = useState("");
|
||||
const router = useRouter();
|
||||
|
||||
const validate = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const token = Cookies.get("token");
|
||||
if (!token) {
|
||||
setLoading(false);
|
||||
setValidated(false);
|
||||
router.push("/");
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await axios.post("/api/user/validate", { token });
|
||||
|
||||
if (response.data.message === "Valid") {
|
||||
setValidated(true);
|
||||
setUsername(response.data.username);
|
||||
setName(response.data.name);
|
||||
} else {
|
||||
setValidated(false);
|
||||
Cookies.remove("token");
|
||||
router.push("/");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Validation error:", error);
|
||||
setValidated(false);
|
||||
Cookies.remove("token");
|
||||
router.push("/");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
useEffect(() => {
|
||||
validate();
|
||||
}, [validate]);
|
||||
|
||||
return {
|
||||
validated,
|
||||
loading,
|
||||
username,
|
||||
name,
|
||||
validate
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export default useAuth;
|
||||
Loading…
x
Reference in New Issue
Block a user