Overview
TheLoginRequired component wraps pages to ensure only authenticated users can access them. It automatically redirects unauthenticated users to /login and shows nothing while the session is loading.
Import
Usage
Wrap any page content that requires authentication:Props
The page content to render once authentication is confirmed
Behavior
Session Loading
While the session status is being determined (isPending is true), the component renders null to prevent flash of content.
Unauthenticated Users
When no authenticated session exists (!session?.user), users are automatically redirected to /login using Next.js router’s replace() method.
Authenticated Users
Once an authenticated session is confirmed, the component renders its children normally.Implementation Details
The component uses:authClient.useSession()from@/lib/auth-clientto get the current session stateuseRouter()fromnext/navigationfor client-side navigationuseEffect()to handle redirects when authentication state changes
Example: Protected Dashboard
login-required.tsx:12-27
Best Practices
Use at Page Level
Wrap entire page components rather than individual sections:Combine with Layout Protection
For apps where most pages require auth, consider usingLoginRequired in the layout:
app/(dashboard)/layout.tsx
Handle Loading States
WhileLoginRequired returns null during loading, you may want to show a loading indicator in your layout:
Related Components
- ThemeProvider - Provides theme context for the app
- AuthStatus - Displays current authentication status