Instagram story viewer> @muazim_codes> Post
4 months ago
12
Posted On: June 20th 2026, 06:12 am
πŸ”’ Protected Routes in React (Clean Implementation)

Not every page should be accessible to everyone.

Protected routes help ensure that only authenticated users can access pages like Dashboard, Profile, Settings, etc.

βœ… Simple and reusable
βœ… Redirects unauthenticated users automatically
βœ… Common pattern used in production applications

Flow:

User visits /dashboard
↓
Token exists?
↓ ↓
Yes No
↓ ↓
Render Redirect
Dashboard to /login

Login Example:

```js
localStorage.setItem("token", "jwt-token");
navigate("/dashboard");
```

Logout Example:

```js
localStorage.removeItem("token");
navigate("/login");
```

This approach uses React Router's Outlet and Navigate components to keep your routing logic clean and scalable.

πŸ’‘ Every React developer should know how protected routes work.

❀️ Like if this helped
πŸ“Œ Save for future reference
πŸ”„ Share with a developer friend
βž• Follow @muazim_codes for daily coding content

Keywords:
React, React Router, Protected Routes, Authentication, JWT, Login, Logout, Route Guards, Frontend Development, JavaScript, Web Development, ReactJS, Software Engineering, Coding Tips, Full Stack Development

#react #reactjs #javascript #webdevelopment #frontend #fullstackdeveloper #programming #coding #developer #softwareengineer #reactrouter #authentication #jwt #codingtips #learnreact #100daysofcode #webdev #programmer #developers #tech
Comments (12)
nikos_michelis 4 months ago

nikos_michelis

For those who blame him, it is just a simple example meant to make you think about how to implement it in your own use case. Also, yeah, the access token must be stored in state and the refresh token as an HttpOnly cookie.

bishalis.dev 4 months ago

bishalis.dev

When vibe coder try to become senior. Do you really think this is good approach?

raccoonpro.academy 4 months ago

raccoonpro.academy

Consider storing a token in http only cookie. Idea is correct, the token location should just be more secure πŸ”. 🦝

_karnel_007 4 months ago

_karnel_007

Can we do like that user && user. role === 'User' && { then here we call all user related route}

mohd_riiyaz 4 months ago

mohd_riiyaz

Best approach is create a /me route that returns userid and role along with authContex and ping that route on protected route visit or even on the first web visit

_zamirovv_01 4 months ago

_zamirovv_01

Looks like junior level. Better make router + protectedRoute files and use them in main.tsx. In production developers use httpsCookies.

sudy4884 4 months ago

sudy4884

This is simple but stupid

shubham_homkar26 4 months ago

shubham_homkar26

This is a very beginners approach. Anyone can add any string to token and hurr rey you logged in πŸ˜‚

zacourious 4 months ago

zacourious

Create a dummy token in localstorage and yea u got into the private app πŸ”₯πŸ”₯

devameer.codes 4 months ago

devameer.codes

This approach is a solid foundation, but a `useNavigate` hook within the protected component itself offers an even cleaner separation of concerns. ✨

More posts

back to up