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
| Option | Type | Description |
|---|---|---|
initialUser | User | null | Seed the user without fetching. |
fetchUser | () => Promise<User> | Fetch the current user from an API. |
immediate | boolean | Fetch immediately on provide. |
storageKey | string | Key used to persist the user. |
Returns
| Key | Type | Description |
|---|---|---|
user | Ref<User | null> | The current user object. |
setUser | (user: User | null) => void | Replace the current user. |
provide | () => void | Provide the context to descendants. |
isAuthenticated | ComputedRef<boolean> | Whether a user is present. |
hasRole / hasAnyRole | functions | Role checks against user.role / user.roles. |
displayName / initials | ComputedRef<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.
