Preparing your workspace
Protected routes check the authenticated user's session before allowing access to private application areas.
Authentication becomes useful only when applications consistently protect private resources. A frontend can check whether a session exists before showing protected UI, while server-side code should independently verify authentication for sensitive operations. In frameworks such as Next.js, the exact session handling depends on whether code runs on the server or browser. The security rule is simple: the client can improve user experience, but sensitive authorization decisions must be enforced by trusted server or database mechanisms.
A student dashboard should require an authenticated session, while public course pages can remain accessible without login.
const {
data: { user }
} = await supabase.auth.getUser();
if (!user) {
throw new Error('Authentication required');
}
console.log(user.id);The authenticated user's ID is available when a valid session exists; otherwise access is rejected.