Skip to content

feat: add withPageAuthRequired for protecting pages client side #2193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

guabu
Copy link
Contributor

@guabu guabu commented Jun 25, 2025

📋 Changes

Adds the withPageAuthRequired HoC to protect pages, client side.

withPageAuthRequired for SSR and APIs will follow in subsequent PRs.

📎 References

🎯 Testing

  1. Create a page, wrapped with withPageAuthRequired (see sample below)
  2. Attempt to access that page without a session
  3. Notice you will be redirected to the login page
"use client";

import Link from "next/link";
import { withPageAuthRequired } from "@auth0/nextjs-auth0";

export default withPageAuthRequired(function Page({ user }) {
  return (
    <div>
      <div>Hello, {user.name}!</div>
      <Link href="/">Home</Link>
    </div>
  );
});

@guabu guabu requested a review from a team as a code owner June 25, 2025 06:28
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.96%. Comparing base (65d682c) to head (ff2ebfa).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2193      +/-   ##
==========================================
+ Coverage   82.91%   83.96%   +1.05%     
==========================================
  Files          21       22       +1     
  Lines        2095     2133      +38     
  Branches      372      386      +14     
==========================================
+ Hits         1737     1791      +54     
+ Misses        351      336      -15     
+ Partials        7        6       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@tusharpandey13 tusharpandey13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add docs in README.md and EXAMPLES.md too?

@guabu
Copy link
Contributor Author

guabu commented Jun 26, 2025

@tusharpandey13 The EXAMPLES.md already contains the newly added section, see: Protecting a Client-Side Rendered (CSR) Page. As well as a callout in the Custom routes section when specifying a custom login route.

Is there anything in particular that you see missing we should include?

@tusharpandey13
Copy link
Contributor

my bad, missed the diff for EXAMPLES.md, this look good 👍

tusharpandey13
tusharpandey13 previously approved these changes Jun 26, 2025
Comment on lines +91 to +92
const currentLocation = window.location;
returnToPath = pathname + currentLocation.search + currentLocation.hash;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason not to use this?

const currentLocation = window.location.toString();
returnToPath = currentLocation.replace(new URL(currentLocation).origin, '') || '/';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.location contains the complete URL path which also includes the base path — this means we would end up with /dashboard/dashboard/profile if the base path was configured as /dashboard and had a returnTo of /dashboard/profile.

We would have to handle this manually, if a base path is configured, which was the first approach I went with: #2193 (comment)

Ultimately, I decided it's cleaner to let Next.js handle this via usePathname since it does not include the base path which avoids having to manually handle the removal of the base path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants