Authentication
Build a simple user authentication system using React Context API. The goal is to understand how to create context, provide context, and consume context within your components, as well as manage basic authentication state globally.
Requirements:
-
UserContext:
-
Create a
UserContextusingReact.createContext(). -
Store the following in the context:
isLoggedIn: A boolean that is false by default, and it toggles between true and false using the login and logout functions.login(): function to log in a user.logout(): function to log out the user.
-
-
App: Wrap the app withUserProvider. -
NavbarComponent:
-
Displays the app title (
App) in the navigation bar. -
Uses
UserContextto accessisLoggedIn,login, andlogout. -
If the user is not logged in:
- Shows a "Login" button that triggers the
loginfunction when clicked.
- Shows a "Login" button that triggers the
-
If the user is logged in:
- Shows a "Welcome, User!" message.
- Displays a "Logout" button that triggers the
logoutfunction when clicked.
DashboardComponent:
-
Uses
UserContextto access theisLoggedInstate. -
If the user is not logged in:
- Displays a message: "Please login to access your dashboard".
-
If the user is logged in:
- Displays the message: "This is your dashboard".
Reference UI

Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
