Real World Example

Role-Aware UI in a Seller Portal

Marketplaces like Flipkart Seller Hub or Amazon Seller Central use one React app for many kinds of staff: some can only see orders, some can edit listings, some can see payouts. The UI hides buttons the user cannot use and the API rejects them anyway. Here is ShopEase's seller dashboard doing both.

// src/pages/SellerDashboard.jsx
import { useEffect, useState } from 'react';
import { api } from '../api';
import { useAuth } from '../auth/AuthContext';

export default function SellerDashboard() {
  const { user, hasRole } = useAuth();
  const [products, setProducts] = useState([]);
  const [message, setMessage] = useState('');

  useEffect(() => {
    api.get('/seller/products').then((res) => setProducts(res.data));
  }, []);

  async function deleteProduct(id) {
    try {
      await api.delete('/products/' + id);
      setProducts((list) => list.filter((p) => p.id !== id));
    } catch (err) {
      if (err.response?.status === 403) setMessage('Only admins can delete products.');
    }
  }

  return (
    <section>
      <h2>Products of {user.email}</h2>
      {message && <p className="error">{message}</p>}
      <table>
        <tbody>
          {products.map((p) => (
            <tr key={p.id}>
              <td>{p.name}</td>
              <td>Rs {p.price}</td>
              <td>{p.stock} in stock</td>
              <td>{hasRole('ROLE_ADMIN') && <button onClick={() => deleteProduct(p.id)}>Delete</button>}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </section>
  );
}

Behind the Scenes

Request

Ravi opens the dashboard; the axios interceptor attaches his token:

GET /api/seller/products HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9...(ravi, ROLE_SELLER)

Example output

HTTP/1.1 200 OK
[{"id":7,"name":"boAt Rockerz 450","price":1399.0,"stock":120},
 {"id":12,"name":"Noise ColorFit Pro 4","price":2499.0,"stock":35}]