add auth and base functions and fixed bug
This commit is contained in:
22
src/core/middlewares/withAuth.js
Normal file
22
src/core/middlewares/withAuth.js
Normal 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
|
||||
Reference in New Issue
Block a user