UNPKG

9s-fe-core

Version:

Core functionalities for authentication, configuration, and repository management.

32 lines (31 loc) 1.29 kB
"use strict"; 'use client'; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const navigation_1 = require("next/navigation"); const AuthProvider_1 = require("./AuthProvider"); const ProtectedRoute = ({ children, redirectPath = '/sign-in' }) => { const { isAuthenticated, isLoading } = (0, AuthProvider_1.useAuth)(); const router = (0, navigation_1.useRouter)(); const pathname = (0, navigation_1.usePathname)(); // Xử lý đường dẫn với locale const getLocalePath = (path) => { const localeMatch = pathname.match(/^\/([a-z]{2})(\/|$)/); const currentLocale = localeMatch ? localeMatch[1] : ''; return currentLocale ? `/${currentLocale}${path}` : path; }; (0, react_1.useEffect)(() => { if (!isLoading && !isAuthenticated) { router.push(getLocalePath(redirectPath)); } }, [isAuthenticated, isLoading, redirectPath, router]); if (isLoading) { return (0, jsx_runtime_1.jsx)("div", { children: "Loading..." }); } if (!isAuthenticated) { return null; } return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }); }; exports.default = ProtectedRoute;