Cherreads

The true story of React, Express.js and MongoDB

PDanac
7
chs / week
The average realized release rate over the past 30 days is 7 chs / week.
--
NOT RATINGS
507
Views
VIEW MORE

Chapter 1 - The Context Story

Chapter 1: Creating the Empty Box createContext() is like building an empty treasure box that can be accessed from anywhere in your React app. This box doesn't contain anything yet - it's just waiting to be filled with data. Think of it as creating a magical container that can teleport its contents to any component that needs it.

Chapter 2: The Provider - The Box Keeper The Provider is like a treasure box keeper. When you wrap components with , you're saying "everything inside this wrapper can access what's in the box." The Provider takes a value prop - this is what gets stored in the box for others to use.

Chapter 3: The Smart AuthProvider Setup Instead of using the raw Provider everywhere, we create a special component called AuthProvider. This is like hiring a brilliant assistant who manages the entire authentication system. The AuthProvider starts by creating two pieces of state: user (who's currently logged in) and loading (whether we're still checking).

Chapter 4: The Initial Check When AuthProvider first runs, it immediately checks if someone was already logged in by looking in localStorage (like checking if someone left their ID card). The loading state starts as true because we need time to do this check. Once we've looked and either found a saved user or confirmed no one was logged in, we set loading to false.

Chapter 5: The Login Function AuthProvider creates a login function - this is like a special procedure for letting someone into the building. When someone provides their credentials (userData), the login function does two things: updates the current user state so the app knows someone is logged in, and saves their information to localStorage so they stay logged in even if they refresh the page.

Chapter 6: The Logout Function AuthProvider also creates a logout function - the opposite of login. This is like an exit procedure that clears everything: it sets the user state back to null (no one is logged in) and removes their information from localStorage (so they won't auto-login next time).

Chapter 7: The Treasure Box Contents AuthProvider puts four things into the treasure box: the current user, the login function, the logout function, and the loading status. This means any component in the app can access not just who's logged in, but also the tools to log in and out.

Chapter 8: The Children Pattern AuthProvider accepts children as a prop. These children are typically your entire app (). The AuthProvider wraps these children with the actual Provider, creating a protective bubble where all the authentication data and functions are available.

Chapter 9: The Execution Flow When your app starts, AuthProvider runs first. It creates state, checks localStorage for existing users, creates the login/logout functions, then returns the Provider wrapper around the children. Only after AuthProvider finishes its complete setup do the children components start running - but they start with full access to authentication.

Chapter 10: The Custom Hook - Your Master Key useAuth() is like a master key that opens the treasure box. Instead of each component remembering the complex useContext(AuthContext) spell, they just use the simple useAuth() key. This custom hook reaches into the box and pulls out the user, login function, logout function, and loading status.

Chapter 11: The Authentication Magic When any component calls useAuth(), it gets everything: const { user, login, logout, loading } = useAuth(). A login page can call login(userData) to sign someone in. A header component can show the user's name and a logout button that calls logout(). A protected route can check if user exists. When any component logs in or out, all other components using useAuth() automatically see the change.

Chapter 12: The Complete Authentication Circle Your main file wraps the entire app with AuthProvider. AuthProvider manages all authentication state, provides login/logout functions, handles localStorage persistence, and puts everything in the Provider box. Any component anywhere in your app can use the useAuth() key to access the current user, log people in, log them out, or check if the system is still loading. The authentication flows seamlessly throughout your entire application, creating a unified login system without any prop drilling.