Unauthorized Component Documentation
Overview
The Unauthorized component displays an access denied message when a user’s role does not permit viewing a page. It shows the current role (if any) and provides a link back to the homepage.
File Location
File Name: Unauthorized.js (or .jsx)
Dependencies
- React: Core UI library.
- React Icons (
react-icons/fa): Icon for the warning symbol (FaExclamationTriangle).
Props
role(string | undefined): The current user role to display. Defaults to'None'if not provided.
Component Structure
- Wrapper
Centers content vertically and horizontally on a full-screen gray background.<div className="flex items-center justify-center min-h-screen bg-gray-100"> - Card
White card with padding, rounded corners, and shadow.<div className="bg-white p-8 rounded-lg shadow-lg max-w-md w-full"> - Icon
Displays a red warning icon inside a circular background.<FaExclamationTriangle className="text-red-500 text-2xl" /> - Heading & Message
- Heading:
<h1>with "Access Denied" - Message:
<p>explaining lack of permission. - Role Display
<div className="bg-gray-100 p-3 rounded-md w-full"> <p className="text-sm text-gray-500">Current role:</p> <p className="font-medium text-gray-700">{role || 'None'}</p> </div> - Return Link
<a href="/" className="bg-blue-500 text-white py-2 px-6 rounded-md hover:bg-blue-600">Return to Homepage</a>
Usage Example
import Unauthorized from './Unauthorized';
function ProtectedRoute({ role }) {
return role === 'admin'
? <AdminDashboard />
: <Unauthorized role={role} />;
}
Notes
- Tailwind CSS classes are used for layout and styling.
- Ensure
react-iconsis installed to render the warning icon.