© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago
Talajax

Creating a component to protect routes

Hi, i created this component to protect the routes
function ProtectedRoute({ children }: { children: JSX.Element }) {
  const { data: user } = useUser();
  const { data: profiles } = useProfiles();
  const router = useRouter();

  if (!user || !profiles) {
    router.push("/");
    return <div></div>;
  }

  return <div>{children}</div>;
}

export default ProtectedRoute;
function ProtectedRoute({ children }: { children: JSX.Element }) {
  const { data: user } = useUser();
  const { data: profiles } = useProfiles();
  const router = useRouter();

  if (!user || !profiles) {
    router.push("/");
    return <div></div>;
  }

  return <div>{children}</div>;
}

export default ProtectedRoute;


And i'm wrapping my components inside this ProtectedRoute component. But it's not working. If i'm at let's say localhost:3000/profiles and i reload the page, it throws an error when it tries to filter profiles. I expect that error since it's not defined, so i redirect to '/' so it can fetch the data. What am i doing wrong here?
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Protect routes in nextjs
SupabaseSSupabase / help-and-questions
4y ago
Best practices to protect routes if sign out fails?
SupabaseSSupabase / help-and-questions
14mo ago
Creating clients for each component
SupabaseSSupabase / help-and-questions
2w ago
Creating a Dashboard
SupabaseSSupabase / help-and-questions
4mo ago