@inertiapixel/nextjs-auth
Version:
Authentication system for Next.js. Supports credentials and social login, JWT token management, and lifecycle hooks — designed to integrate with nodejs-auth for full-stack MERN apps.
21 lines (20 loc) • 714 B
JavaScript
'use client';
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useAuth } from '../../hooks/useAuth';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
const Protect = ({ children, loadingFallback = null, redirectTo = '/login' }) => {
const { user, loading } = useAuth();
const router = useRouter();
useEffect(() => {
if (!loading && !user) {
router.replace(redirectTo);
}
}, [loading, user, router, redirectTo]);
if (loading)
return _jsx(_Fragment, { children: loadingFallback });
if (!user)
return null;
return _jsx(_Fragment, { children: children });
};
export default Protect;