Auth Guard in Next.js

Auth Guard protects routes from unauthorized access.

Definition

Auth Guard is a logic pattern used to restrict access to certain pages based on authentication.

Why We Use

When To Use

When only logged-in users should access a route.

Example Code

import { redirect } from "next/navigation";

export default function Dashboard({ user }) {
  if (!user) {
    redirect("/login");
  }

  return <h1>Welcome</h1>;
}

Real-World Example

Banking dashboard accessible only after login.

Interview Tip

Auth Guard restricts route access using conditional redirect logic.