Global User Object
The global user object in your application is a central piece of state management, designed to store and provide access to the current user’s information across the application. It is structured as follows:
_id: A unique identifier for the user.first_name: The user’s first name.last_name: The user’s last name.email: The user’s email address.is_admin: A boolean indicating if the user has administrative privileges.token: A JWT token used for authenticating API requests.company: The name of the company the user belongs to.companyAddress: The address of the user’s company.
This object is stored in the UserContext, making it accessible throughout the application’s component tree. It is initially set by retrieving a stored user from localStorage, parsed, and then maintained in the application’s state using React’s useState hook. This ensures that user data is persistently available during the user session, even after page refreshes.
The UserContext provides a way to manage and share this user data without having to prop-drill it through multiple components, streamlining the process of managing authenticated user state.