"use client"; import { usePathname, useRouter } from "next/navigation"; import type { ProviderProps } from "@/@types/types/provider-props.types"; import { useAuth } from "@/features/auth/hooks/use-auth"; import { useApp } from "@/hooks/useApp"; import { useCore } from "./use-core"; const locales = ["en", "tr"]; const publicPages = ["/auth", "/resetpassword", "/auth/forgot-password"]; const publicPathnameRegex = RegExp( `^(/(${locales.join("|")}))?(${publicPages .flatMap((p) => (p === "/" ? ["", "/"] : p)) .join("|")})/?$`, "i", ); const CoreProvider = (props: ProviderProps) => { const { isB2E } = useApp(); const { isAuthenticated } = useAuth(); const pathname = usePathname(); const router = useRouter(); useCore(); if (isB2E) { const isPublicPage = publicPathnameRegex.test(pathname); if (!isAuthenticated && !isPublicPage) { router.push("/auth"); return
; } } else { const isPublicPage = publicPathnameRegex.test(pathname); if (isPublicPage) { router.push("/"); return ; } } return props.children; }; export default CoreProvider;