-
Book Overview & Buying
-
Table Of Contents
React Application Architecture for Production - Second Edition
By :
In this chapter, we implemented a complete authentication and authorization system and secured our application against common threats. We started by extending our API client to handle authentication tokens automatically, including automatic token refresh when access tokens expire. This provides a seamless user experience where users stay logged in without interruption.
We built registration, login, and logout flows that use httpOnly cookies to store authentication tokens securely. These cookies can't be accessed by JavaScript, protecting them from XSS attacks. We created middleware to load the current user on every request and made the user available throughout the application with a simple useUser hook.
To protect sensitive routes, we implemented a protected middleware that redirects unauthenticated users to the login page. By adding this middleware to layout components, we can protect entire sections of our application with a single line of code.
We implemented...