Authentication & Routing
Protected Routes
SREDSimplify utilizes High-Order Components (HOCs) to guard routes based on authentication status and user roles. These are located in src/components/auth/.
<ProtectedRoute>: Ensures the user has a valid JWT token. If not, redirects to/login. Wraps the entire(app)route group.<AdminRoute>: Checks the user's role against theADMINtier. Wraps the/admin/*routes. If a standard user attempts access, they are redirected.<GuestRoute>: Ensures the user is not logged in. Wraps routes like/loginand/register. If an authenticated user visits these, they are redirected to/projects.
JWT Lifecycle
- Upon successful login/registration, the backend returns a JWT.
- The
authStore(Zustand) saves this token tolocalStorage. - The Axios interceptor attaches the token to the
Authorizationheader asBearer <token>on every outbound request. - If the backend returns
401 Unauthorized(e.g., token expiration), Axios intercepts the response, triggersauthStore.logout(), and pushes the router to/login.