feat(zustand): add zustand stores for exam, timer and auth

This commit is contained in:
shafin-r
2025-08-31 18:28:01 +06:00
parent 65e3338859
commit 7df2708db7
18 changed files with 352 additions and 106 deletions

View File

@ -0,0 +1,40 @@
"use client";
import { useEffect } from "react";
import { useAuthStore } from "@/stores/authStore";
import { useRouter, usePathname } from "next/navigation";
export default function AuthInitializer({
children,
}: {
children: React.ReactNode;
}) {
const { initializeAuth, isLoading, token } = useAuthStore();
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
const run = async () => {
await initializeAuth();
// Routing logic based on auth state
const publicPages = ["/", "/login", "/register"];
if (token) {
if (publicPages.includes(pathname)) {
router.replace("/home");
}
} else {
if (!publicPages.includes(pathname)) {
router.replace("/");
}
}
};
run();
}, [pathname, token, initializeAuth, router]);
if (isLoading) return <p>Loading...</p>;
return <>{children}</>;
}