How do you handle authentication state in a MERN frontend?
Model answer
Keep the signed-in user in Context (or a store), load it on startup from a /me endpoint, and protect routes with a wrapper that redirects to login. Handle 401 responses globally by clearing state and redirecting. The UI check is only for experience, and the server still enforces access.
Learn this topic
useEffect and Fetching Data from Your API: Loading, Errors and Cleanup in the course MERN Stack Full Course: MongoDB, Express, React and Node.js.
Sign in to add your own answer.
More questions
- How does a React app call an Express API?
- How do you set up a Vite proxy for development?
- How do you serve a React build from Express?
- How do you handle loading, error and empty states for API data in React?
- How do you build a dashboard with filters and pagination backed by the API?
- How do you share validation and types between frontend and backend?