Auth Context on Front End

Purpose: In the previous two exercises, you made login and logout trigger changes (in the Nav and Secret page) on the front end. You made a shared javascript file (auth.js) that used localStorage to hold the login state and login information. In this exercise, you will add a layer of shared state variables (with React's Context API), which offers greater flexibility and integration.

Problem: Login State is Checked Only on Page Navigation. If you consider carefully, you might notice how important Page Navigation is to updating how the current React app looks when login changes. When Login completes, it is only after useNavigate makes React Router send the user somewhere that the Nav bar and Secret page change. Similarly, when the Logout button is pressed, it is only because its NavLink component makes React Router send the user somewhere that the Nav bar changes again. When the "Secret Identity" NavLink is pressed, the NavLink component does the same.

Problem: When Login Expires, User Doesn't Know. You might also notice that when the login expires, the user does not know about it until a Page Navigation happens. Imagine the user closing the computer for two hours and comes back to the site. The login is expired, but the user does not find out until a link is clicked. In this React App, none of this seems to be a big problem, because pages are simple, and the user navigates pages a lot. However, you might feel differently if the Login component did not do Page Navigation (for example, if it were in a dialog box or part of a page and stayed on the same page). It might be better to force components to change based on a button click or a timer, something other than Page Navigation. React's "state variables" provide an elegant solution to this problem, since changing a state variable forces a component to update.

Local State vs. Shared State. One problem with useState variables is that they are local to a single component. If we update a state variable in the Nav component, the App component (a parent of a parent) can't see it. If we update a state variable in the App component, the Nav component can only see it if its set function is passed down using prop drilling. React's Context API provides a way to share state variables with an entire section of the DOM tree.

How the Context API Works

When creating a context, you basically create a context object and a component from that context object. The component will be wrapped around an area of the DOM tree. After that happens, the context object can be imported and share state variables and functions using the useContext function. Here's how it looks to create a context object and its component (usually done in its own file):

ContextName.jsx

Here's how it looks to wrap the context (in this case, wrapped around the whole app in main.jsx, but it could wrap around any part of the DOM tree):

main.jsx

Here's how it looks to use the context in any component inside the wrapping is:

In any Component File

Create AuthContext

Create a basic AuthContext that uses the functions of auth.js:

AuthContext.jsx

Now, wrap the AuthContext tag around the entire app and BrowserRouter:

main.jsx

To separate concerns properly, all code from auth.js should be accessed only through AuthContext. Do this in Nav.jsx, which needs onyl the level and logout functions.

Nav.jsx
Remove auth.js import and add AuthContext import:
Remove:
And change:
to:
Finally, in the conditional rendering near the bottom, change:
{level ?
to:
{level() ?
because level is now a function in AuthContext.

Do the same in Login.jsx:

Login.jsx
Remove auth.js import and add AuthContext import:

In addition, to keep the Login page simple and high level, move the following code from Login.jsx to AuthContext.jsx:

Move most of submit_login from Login to AuthContext:
AuthContext.jsx
Keep the following in submit_login:
Login.jsx

Understand the Code

Take a moment to look at the following important features of the above code and internalize what each means:

Add a Client-Side Timeout Check

To demonstrate why it was a good idea to make login dependent on something other than Page Navigation, add useEffect to AuthContext that checks token expiration at the expected time of expiration (+ 100 milliseconds to be sure). The Javascript setTimeout function allows you to run a function after a specified number of milliseconds:

AuthContext_jsx3.txt

To test the code above, you may wish to reconfigure the server to make shorter login token expirations. It is optional on your project to have front end login timeout features like this one (or more complex ones), but setting up an Auth Context is required.