Skip to content

feat(routes): bounce logged-in users away from auth pages#33

Merged
hoangsonww merged 2 commits into
masterfrom
feat/jwt-polling-loading-ux
May 23, 2026
Merged

feat(routes): bounce logged-in users away from auth pages#33
hoangsonww merged 2 commits into
masterfrom
feat/jwt-polling-loading-ux

Conversation

@hoangsonww
Copy link
Copy Markdown
Owner

This pull request introduces a new GuestRoute component to ensure that only unauthenticated users can access the login, registration, and forgot password pages. This helps prevent logged-in users from accessing pages meant for guests, improving both security and user experience. The main changes are the creation of the GuestRoute component and its integration into the routing logic.

Authentication and Routing Improvements:

  • Added a new GuestRoute component in frontend/src/components/GuestRoute.js that redirects authenticated users away from guest-only routes.
  • Updated routing in frontend/src/App.js to wrap the /login, /register, and /forgot-password routes with GuestRoute, ensuring only unauthenticated users can access these pages. [1] [2]
  • Imported the new GuestRoute component into frontend/src/App.js.

Hitting /login, /register, or /forgot-password while already
authenticated is a dead end — redirect to /. Mirrors ProtectedRoute
with the opposite predicate.

Co-authored-by: David Nguyen <hoangson091104@gmail.com>
@hoangsonww hoangsonww added this to the v1.0.0 - Stable Release milestone May 23, 2026
@hoangsonww hoangsonww self-assigned this May 23, 2026
Copilot AI review requested due to automatic review settings May 23, 2026 22:51
@hoangsonww hoangsonww added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 23, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
budget-management-backend-api Ignored Ignored May 23, 2026 10:52pm

@netlify
Copy link
Copy Markdown

netlify Bot commented May 23, 2026

Deploy Preview for budget-management-system ready!

Name Link
🔨 Latest commit 0fc4eab
🔍 Latest deploy log https://app.netlify.com/projects/budget-management-system/deploys/6a122f84e06e120008a4ae1d
😎 Deploy Preview https://deploy-preview-33--budget-management-system.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@hoangsonww hoangsonww merged commit bff7cfd into master May 23, 2026
3 checks passed
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a GuestRoute component to protect guest-only routes such as login, registration, and forgot-password, redirecting authenticated users to a default path. Feedback was provided to improve the GuestRoute implementation by making it reactive to authentication state changes using a useEffect hook and the onAuthChange utility, rather than relying on a static initial check.

Comment on lines +1 to +12
import React from 'react';
import { Navigate } from 'react-router-dom';
import { isLoggedIn } from '../services/auth';

function GuestRoute({ children, redirectTo = '/' }) {
if (isLoggedIn()) {
return <Navigate to={redirectTo} replace />;
}
return children;
}

export default GuestRoute;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current implementation of GuestRoute is static and only checks the authentication status during the initial render. This means the component won't automatically react if the authentication state changes (for example, if a user logs out in another tab or if the session expires) unless a manual refresh or navigation occurs. Since auth.js provides an onAuthChange utility, it is recommended to use it within a useEffect hook to make the component reactive to authentication state transitions.

import React, { useState, useEffect } from 'react';
import { Navigate } from 'react-router-dom';
import { isLoggedIn, onAuthChange } from '../services/auth';

function GuestRoute({ children, redirectTo = '/' }) {
  const [authenticated, setAuthenticated] = useState(isLoggedIn());

  useEffect(() => {
    // Subscribe to auth changes to ensure the component reacts to login/logout events
    return onAuthChange(() => {
      setAuthenticated(isLoggedIn());
    });
  }, []);

  if (authenticated) {
    return <Navigate to={redirectTo} replace />;
  }
  return children;
}

export default GuestRoute;

@netlify
Copy link
Copy Markdown

netlify Bot commented May 23, 2026

Deploy Preview for budget-management-system ready!

Name Link
🔨 Latest commit 9705e98
🔍 Latest deploy log https://app.netlify.com/projects/budget-management-system/deploys/6a122fb24aa8520007960ae7
😎 Deploy Preview https://deploy-preview-33--budget-management-system.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown

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 introduces a GuestRoute wrapper to prevent authenticated users from accessing guest-only authentication pages (login, registration, forgot password), improving routing correctness and UX consistency.

Changes:

  • Added frontend/src/components/GuestRoute.js to redirect authenticated users away from guest-only routes.
  • Updated frontend/src/App.js to wrap /login, /register, and /forgot-password routes with GuestRoute.
  • Integrated the new component into the app’s route configuration via import and usage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
frontend/src/components/GuestRoute.js Adds a guest-only route guard that redirects when isLoggedIn() is true.
frontend/src/App.js Applies GuestRoute to auth-related routes to bounce authenticated users away from guest pages.

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

Comment thread frontend/src/App.js
Comment on lines +47 to +54
<Route
path="/login"
element={
<GuestRoute>
<Login />
</GuestRoute>
}
/>
@hoangsonww hoangsonww moved this from Done to In review in Budget Management API Project Board May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants