X Enterprises
Composables

useCurrentUser

Current user state with provide/inject sharing, role-checking utilities, and optional auth fetch integration.

useCurrentUser

Lightweight current-user state. Call it once in a root component or plugin with options to provide the user context, then call it without options anywhere below to consume it. Includes role checks used by XAPermissionGate and the user menu components.

Usage

// In a root component or plugin — provide the user context
const { user, setUser, provide } = useCurrentUser({
  fetchUser: () => $fetch('/api/auth/me'),
  immediate: true,
  storageKey: 'currentUser',
})
provide()

// In any child component — consume the user context
const { user, hasRole, hasAnyRole, isAuthenticated, displayName, initials } = useCurrentUser()

if (hasRole('admin')) { /* ... */ }
if (hasAnyRole(['admin', 'editor'])) { /* ... */ }

Options

OptionTypeDescription
initialUserUser | nullSeed the user without fetching.
fetchUser() => Promise<User>Fetch the current user from an API.
immediatebooleanFetch immediately on provide.
storageKeystringKey used to persist the user.

Returns

KeyTypeDescription
userRef<User | null>The current user object.
setUser(user: User | null) => voidReplace the current user.
provide() => voidProvide the context to descendants.
isAuthenticatedComputedRef<boolean>Whether a user is present.
hasRole / hasAnyRolefunctionsRole checks against user.role / user.roles.
displayName / initialsComputedRef<string>Display helpers for avatars and menus.

AI Context

composable: useCurrentUser
package: "@xenterprises/nuxt-x-app"
use-when: Accessing the signed-in user's profile or gating UI by role in dashboard pages.
Copyright © 2026