add auth and base functions and fixed bug

This commit is contained in:
Amirhossein Mahmoodi
2024-07-09 11:28:59 +03:30
parent 6288ac926c
commit 863bdaca95
23 changed files with 592 additions and 69 deletions

View File

@@ -0,0 +1,22 @@
'use client'
import { useAuth } from "@/lib/contexts/auth";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
function WithAuthMiddleware({ children }) {
const router = useRouter()
const pathName = usePathname()
const { isAuth, initAuthState } = useAuth();
useEffect(() => {
if (!initAuthState) return
if (!isAuth) {
router.replace(`${process.env.NEXT_PUBLIC_API_URL}/login?_back=${encodeURIComponent(pathName)}`)
}
}, [isAuth, initAuthState])
if (!initAuthState || !isAuth) return null;
return <>{children}</>
}
export default WithAuthMiddleware