Skip to main content

User Context

The User Context is a crucial part of the application that provides access to user-related information throughout the app. It uses React's Context API to make user data and authentication status available to all components without prop drilling.

Key Features

  • Authentication Status: Tracks whether the user is logged in, loading, or unauthenticated.
  • User Data: Provides access to the current user's information.
  • Role Checking: Allows checking if the user has a specific role.
  • Organization Support: If the organization module is active, it provides additional organization-related data.

Usage

To use the User Context in your components:

import { useUserContext } from '@/core/context'

export default function HomePage() {
const { user, checkRole } = useUserContext()

return (
<div>
<h1>Welcome, {user.name}!</h1>
{checkRole('admin') && <div>I am a an admin!</div>}
</div>
)
}

Available Properties

  • user: The current user object (if logged in)
  • isLoggedIn: Boolean indicating if the user is authenticated
  • checkRole: Function to check if the user has a specific role
  • authenticationStatus: Current authentication status ('unauthenticated', 'loading', or 'authenticated')
  • isLoading: Boolean indicating if user data is still loading
  • refetch: Function to manually refetch user data

When the organization module is active, additional properties are available:

  • organization: Current organization data
  • organizationRoles: User's roles in the current organization
  • organizations: All organizations associated with the user
  • checkOrganizationRole: Function to check user's role in the current organization