Authentication
React.js
easy
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
UserContext
usingReact.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
. -
Navbar
Component:
-
Displays the app title (
App
) in the navigation bar. -
Uses
UserContext
to accessisLoggedIn
,login
, andlogout
. -
If the user is not logged in:
- Shows a "Login" button that triggers the
login
function 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
logout
function when clicked.
Dashboard
Component:
-
Uses
UserContext
to access theisLoggedIn
state. -
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
Preview what you need to build
Companies:
zoom
amazon
Solve Similar questions 🔥