Skip to main content

Featured

🎉 Day 46 — React Mastery Completed (Final Summary & Congratulations!)

🎉 Day 46 — React Mastery Completed (Final Summary & Congratulations!) Congratulations, developer! 👏 You’ve successfully completed the 45-Day React.js Roadmap — from understanding the fundamentals to mastering advanced concepts like Redux, Routing, Testing, and Deployment. 📘 What You’ve Learned ✅ React basics — Components, JSX, Props, and State ✅ Hooks — useState, useEffect, useRef, useMemo, and Custom Hooks ✅ Context API and Redux Toolkit for global state management ✅ Routing with React Router & Protected Routes ✅ Data fetching using Fetch API, Axios, React Query ✅ Advanced Patterns — Compound Components, Render Props, HOCs ✅ Styling — CSS Modules, Styled Components, Theming ✅ Animations, Accessibility, Testing, and Performance Optimization ✅ Deployment on Vercel, Netlify, or GitHub Pages 🧩 Final Project Ideas Now that you’re done, build real-world apps to polish your skills: 📝 Task ...

🔐 Day 32 — Authentication Patterns (JWT & Refresh Tokens)

🔐 Day 32 — Authentication Patterns (JWT & Refresh Tokens)

Authentication is a vital part of modern React apps. JSON Web Tokens (JWT) are commonly used to securely transmit user data and validate sessions.

🔹 How JWT Works

  • User logs in with credentials.
  • Server returns a signed JWT and refresh token.
  • JWT is stored on the client (usually in localStorage).
  • Client sends JWT with each request for authentication.
  • When JWT expires, the refresh token is used to get a new one.

💻 Example: Login with JWT

async function loginUser(credentials) {
  const response = await fetch("/api/login", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(credentials),
  });
  const data = await response.json();
  localStorage.setItem("token", data.accessToken);
}
  

The JWT is stored securely and attached to all future API requests. You can use Axios interceptors to automatically add it to request headers.

🔒 Protecting Routes

import { Navigate } from "react-router-dom";

function ProtectedRoute({ children }) {
  const token = localStorage.getItem("token");
  return token ? children : <Navigate to="/login" />;
}
  

Always use HTTPS and avoid storing sensitive data in localStorage. For enhanced security, prefer HttpOnly cookies with refresh token rotation.

Comments