useAuth Integration

This commit is contained in:
headlesdev
2025-05-20 00:52:17 +02:00
parent cc59988917
commit 76ec9dad93
5 changed files with 33 additions and 81 deletions

View File

@@ -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;