Skip to content

[DEV-3756] Add redirect in middleware for authenticated user navigating to auth pages#2099

Merged
marcobottaro merged 11 commits intomainfrom
DEV-3756-redirect-authenticated-user-on-auth-routes
Mar 17, 2026
Merged

[DEV-3756] Add redirect in middleware for authenticated user navigating to auth pages#2099
marcobottaro merged 11 commits intomainfrom
DEV-3756-redirect-authenticated-user-on-auth-routes

Conversation

@Sebastiano-Bertolin
Copy link
Copy Markdown
Collaborator

This pull request refactors the authentication redirect logic for guest pages by moving it from a React hook into middleware, ensuring unauthenticated users are properly redirected at the server level. It also improves the Amplify configuration to set cookie storage options dynamically based on the environment. The most important changes are grouped below:

List of Changes

Authentication redirect logic:

  • Removed the useAuthenticatedUserRedirect React hook from user.helper.ts, eliminating client-side checks and redirects for authenticated users on guest pages.
  • Removed usage of useAuthenticatedUserRedirect from the sign-up page component, simplifying the component logic.
  • Added new middleware logic in middleware.ts to detect authenticated users accessing guest pages and redirect them to the locale root server-side, using the Cognito cookie for detection.

Amplify configuration improvements:

  • Updated AuthProvider.tsx to configure Amplify with dynamic cookie storage settings when running in the browser, ensuring cookies are set correctly per domain and protocol.

Minor cleanup:

  • Removed unused import of redirect from user.helper.ts.

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Chore (nothing changes by a user perspective)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

…ponents and implement middleware for authenticated user redirection
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 1d4c9dd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
nextjs-website Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR moves “authenticated user on guest/auth pages” redirect logic from client-side React hooks into Next.js middleware, and adjusts Amplify’s cookie storage configuration so auth cookies are set with environment-derived options in the browser.

Changes:

  • Add middleware logic to detect an authenticated user (via Cognito cookie) visiting auth/guest routes and redirect them to the locale root.
  • Remove the useAuthenticatedUserRedirect hook and its usage in the sign-up page.
  • Configure Amplify Auth cookie storage dynamically (domain/secure) in the browser.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
apps/nextjs-website/src/middleware.ts Adds guest-route detection and cookie-based redirect; keeps existing locale rewrite behavior.
apps/nextjs-website/src/helpers/user.helper.ts Removes the client-side redirect hook and an unused import.
apps/nextjs-website/src/components/organisms/Auth/AuthProvider.tsx Configures Amplify with runtime cookieStorage options in the browser.
apps/nextjs-website/src/app/[locale]/auth/sign-up/page.tsx Removes the authenticated-user redirect hook usage and related “loading” gating.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@MarcoPonchia MarcoPonchia left a comment

Choose a reason for hiding this comment

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

Changeset is missing

Copy link
Copy Markdown
Collaborator

@marcobottaro marcobottaro left a comment

Choose a reason for hiding this comment

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

If an authenticated user tries to navigate to /auth/sign-up (without the locale), the browser redirects to https://${locale}/ without the base path

@github-actions
Copy link
Copy Markdown
Contributor

Branch is not up to date with base branch

@Sebastiano-Bertolin it seems this Pull Request is not updated with base branch.
Please proceed with a merge or rebase to solve this.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 17, 2026

Jira Pull Request Link

This Pull Request refers to the following Jira issue DEV-3756

@marcobottaro marcobottaro merged commit 1c4434b into main Mar 17, 2026
14 checks passed
@marcobottaro marcobottaro deleted the DEV-3756-redirect-authenticated-user-on-auth-routes branch March 17, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants