Learn how to secure certain parts of your application using protected routes.
Protected routes ensure only authenticated users can access specific pages or content.
Protected routes are sections of your application that are accessible only to authenticated users. They prevent unauthorized users from accessing sensitive pages, ensuring application security and a streamlined user experience.
The useAuth
hook determines whether a user is authenticated:
const isAuthenticated = useAuth();
It returns true
if the user is authenticated and false
otherwise.
This right now assumes that there inside routes are rendered via client side rendering.
Based on the authentication status, the component decides what to render:
if (!isAuthenticated) {
return <DashboardSkeleton />;
}
If the user is not authenticated, a placeholder (e.g., DashboardSkeleton
) is displayed until authentication is resolved or the user is redirected.
Once authenticated, the protected content and layout are rendered:
if (isAuthenticated) {
return <ProtectedContent />;
}
In addition to placeholders like DashboardSkeleton
the above hook will redirect the user automatically to the homepage if he is not logged in.
For more detailed information, refer to the documentation or consult your authentication provider's guide.