Auth Check or Route Guard
components/AuthCheck.js
import Link from 'next/link';
import { useContext } from 'react';
import { UserContext } from '../lib/context';
// Component's children only shown to logged-in users
export default function AuthCheck(props) {
const { username } = useContext(UserContext);
return username ? props.children : props.fallback || <Link href="/enter">You must be signed in</Link>;
}
Usage in a Component
admin/index.js
import AuthCheck from '../../components/AuthCheck';
export default function AdminPostsPage(props) {
return (
<main>
<AuthCheck>
</AuthCheck>
</main>
);
}