Protect Client Rendered Pages

What you'll cover in this guide

  • ✅ Create a client rendered page and protect it using protectPage()
  • ✅ Customize the return URL
  • ✅ Set up error handling
Using the Next.js App Router instead? See the App Router version of this guide.

Before you get started

This guide assumes you have a Next.js project set up and running. If you haven't set up authentication in your Next.js App Router project yet, please refer to the quickstart guide.

Create and protect a client rendered page

We'll start by setting up our client-rendered page and making sure it's only accessible to authenticated users.

Open src/pages/index.tsx in your editor and replace the contents with the following code:

src/pages/index.tsx
import { MonoCloudUser } from "@monocloud/nextjs-auth";
import { SignOut } from "@monocloud/nextjs-auth/components";
import { protectPage } from "@monocloud/nextjs-auth/client";

const Home = (props: { user: MonoCloudUser }) => {
  return (
    <div className="flex flex-col gap-3">
      <p>Hi {props.user.email} from the Client Rendered Page</p>
      <SignOut>Sign Out</SignOut>
    </div>
  );
};

export default protectPage(Home);

Let's review what this code does:

  1. We imported the protectPage() function from @monocloud/nextjs-auth/client which protects the page by redirecting the user to the Sign In page if they're not already authenticated. It also injects the authenticated user's profile data as a prop to the page component.
  2. We use the user property to retrieve the user's email and display a customized greeting to the authenticated user, along with a Sign Out button.
  3. Lastly, we wrap and export the client-rendered page with the protectPage() function to ensure it is only accessible to authenticated users.

See in action by running:

Terminal
npm run dev

Then visit http://localhost:3000. Since we protected the root page, you'll be redirected to the sign-in page. Sign in and then try signing out.

Customizing the return URL

After sign-in, the default behavior is to redirect the user back to the page which triggered the sign-in process.

You can change that by passing the returnUrl option to the protectPage() function, which will determine where to redirect the user after a successful sign-in.

Let's try it in the page we just created. Add { returnUrl: 'custom' } as the second parameter to the protectPage() function:

src/pages/index.tsx
import { MonoCloudUser } from "@monocloud/nextjs-auth";
import { SignOut } from "@monocloud/nextjs-auth/components";
import { protectPage } from "@monocloud/nextjs-auth/client";

const Home = (props: { user: MonoCloudUser }) => {
  return (
    <div className="flex flex-col gap-3">
      <p>Hi {props.user.email} from the Client Rendered Page</p>
      <SignOut>Sign Out</SignOut>
    </div>
  );
};

export default protectPage(Home, { returnUrl: "custom" });

Now, after a successful sign-in, the page will redirect to http://localhost:3000/custom.

Error handling

You can handle authentication errors by configuring the onError option in the protectPage() function. The onError function receives an Error object as an argument, which contains the cause of the error.

Let's add a simple error message to our page by modifying src/pages/index.tsx as follows:

src/pages/index.tsx
import { MonoCloudUser } from "@monocloud/nextjs-auth";
import { SignOut } from "@monocloud/nextjs-auth/components";
import { protectPage } from "@monocloud/nextjs-auth/client";

type HomePageProps = { user: MonoCloudUser };

const Home = (props: { user: MonoCloudUser }) => {
  return (
    <div className="flex flex-col gap-3">
      <p>Hi {props.user.email} from the Client Rendered Page</p>
      <SignOut>Sign Out</SignOut>
    </div>
  );
};

export default protectPage(Home, {
  onError: (e: Error) => <>Error: {e.message}</>,
});

If an error occurs during authentication, the page will display the cause of the error to help users debug any issues.