Skip to content

Create private route component Issue #230 resolved #274

Open
teenananana wants to merge 4 commits intolugenx:mainfrom
teenananana:create-private-route-component
Open

Create private route component Issue #230 resolved #274
teenananana wants to merge 4 commits intolugenx:mainfrom
teenananana:create-private-route-component

Conversation

@teenananana
Copy link

Description

Implemented a PrivateRoute component to protect routes requiring authentication in the EcoHabit application.

Changes Made

  • Created PrivateRoute component in client/src/components/PrivateRoute/PrivateRoute.jsx
  • Added index file for clean imports (client/src/components/PrivateRoute/index.js)
  • Integrated PrivateRoute into the routing configuration in Routes.js
  • Protected routes: /profile, /logout, and /Demo now require authentication
  • Public routes remain accessible without login: home, locations, guides, about, login, terms

Implementation Details

  • Uses React Router v6's Outlet component for nested route rendering
  • Checks authentication via localStorage token (authToken)
  • Redirects unauthenticated users to /login page
  • Preserves attempted location for post-login redirect using useLocation
  • Fully compatible with existing createBrowserRouter structure

How to Test

  1. Start the application (backend and frontend)
  2. Without logging in, try accessing /profile - should redirect to /login
  3. Try accessing /Demo without auth - should redirect to /login
  4. Log in with valid credentials
  5. After login, verify you can access /profile, /logout, and /Demo
  6. Log out and verify you're redirected when trying to access protected routes again

Related Issue

Fixes #230

@teenananana teenananana requested a review from lugenx as a code owner November 3, 2025 06:15
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.

Create Private Route Component

1 participant