Composables
useOrganization
Better Auth Organizations & Teams composable for nuxt-x-auth-better — org management, members, invitations, teams, and access control.
useOrganization
Better Auth organizations and teams composable for the nuxt-x-auth-better layer. Wraps the Better Auth organizationClient with reactive state for the user's organizations, active organization, members, invitations, and teams. All organization and team UI components in this layer consume useOrganization internally.
Usage
const {
isEnabled,
teamsEnabled,
config,
organizations,
organizationsLoading,
organizationsError,
activeOrganization,
activeLoading,
activeError,
activeMember,
activeMemberRole,
members,
invitations,
teams,
canCreateOrganization,
isOrgAdmin,
isOrgOwner,
createOrganization,
updateOrganization,
deleteOrganization,
setActiveOrganization,
getFullOrganization,
checkSlug,
listMembers,
updateMemberRole,
removeMember,
leaveOrganization,
inviteMember,
cancelInvitation,
acceptInvitation,
rejectInvitation,
getInvitation,
listInvitations,
listUserInvitations,
listTeams,
createTeam,
updateTeam,
deleteTeam,
setActiveTeam,
listTeamMembers,
addTeamMember,
removeTeamMember,
listUserTeams,
hasPermission,
checkRolePermission,
getDefaultPermissions,
authClient,
} = useOrganization()
Returns
Feature Flags
| Key | Type | Description |
|---|---|---|
isEnabled | ComputedRef<boolean> | true when features.organization is enabled in app.config.ts and organization.enabled is not false. |
teamsEnabled | ComputedRef<boolean> | true when organizations are enabled and organization.teams is not false. |
config | ComputedRef<OrganizationConfig> | Reactive reference to the xAuth.organization app config from app.config.ts. |
State
| Key | Type | Description |
|---|---|---|
organizations | ComputedRef<Organization[]> | All organizations the current user belongs to. |
organizationsLoading | ComputedRef<boolean> | true while the organization list is being fetched. |
organizationsError | ComputedRef<Error | null> | Any error returned while fetching the organization list. |
activeOrganization | ComputedRef<Organization | null> | The currently active organization. |
activeLoading | ComputedRef<boolean> | true while the active organization is being resolved. |
activeError | ComputedRef<Error | null> | Any error returned while resolving the active organization. |
activeMember | ComputedRef<OrganizationMember | null> | The current user's member record in the active organization. |
activeMemberRole | ComputedRef<string | null> | The current user's role in the active organization. |
members | ComputedRef<OrganizationMember[]> | Members of the active organization. |
invitations | ComputedRef<OrganizationInvitation[]> | Pending invitations in the active organization. |
teams | ComputedRef<OrganizationTeam[]> | Teams in the active organization. |
canCreateOrganization | ComputedRef<boolean> | true unless organization.allowUserToCreateOrganization is false. |
isOrgAdmin | ComputedRef<boolean> | true when the active member role is owner or admin. |
isOrgOwner | ComputedRef<boolean> | true when the active member role is owner. |
Organization Methods
| Key | Type | Description |
|---|---|---|
createOrganization | (name: string, slug?: string, logo?: string) => Promise<Organization | null | { error: string }> | Creates a new organization and optionally sets a slug and logo. |
updateOrganization | (data: { organizationId?: string; name?: string; slug?: string; logo?: string }) => Promise<Organization | null | { error: string }> | Updates an organization's name, slug, or logo. |
deleteOrganization | (organizationId?: string) => Promise<boolean | { error: string }> | Deletes an organization. |
setActiveOrganization | (organizationId?: string | null, organizationSlug?: string) => Promise<Organization | null | { error: string }> | Sets the active organization by ID or slug. |
getFullOrganization | (organizationId?: string, organizationSlug?: string) => Promise<Organization | null | { error: string }> | Fetches the full organization record including members, invitations, and teams. |
checkSlug | (slug: string) => Promise<{ status: boolean } | null | { error: string }> | Checks whether an organization slug is available. |
Member Methods
| Key | Type | Description |
|---|---|---|
listMembers | (organizationId?: string) => Promise<OrganizationMember[] | null | { error: string }> | Lists members of the active or specified organization. |
updateMemberRole | (memberId: string, role: string | string[], organizationId?: string) => Promise<OrganizationMember | null | { error: string }> | Updates a member's role. |
removeMember | (memberId: string, organizationId?: string) => Promise<{ success: boolean } | { error: string }> | Removes a member from the organization. |
leaveOrganization | () => Promise<{ success: boolean } | { error: string }> | Removes the current user from the active organization. |
Invitation Methods
| Key | Type | Description |
|---|---|---|
inviteMember | (email: string, role: string | string[], organizationId?: string) => Promise<OrganizationInvitation | null | { error: string }> | Invites a user to the active or specified organization. |
cancelInvitation | (invitationId: string) => Promise<{ success: boolean } | { error: string }> | Cancels a pending invitation. |
acceptInvitation | (invitationId: string) => Promise<{ success: boolean } | { error: string }> | Accepts an organization invitation. |
rejectInvitation | (invitationId: string) => Promise<{ success: boolean } | { error: string }> | Rejects an organization invitation. |
getInvitation | (invitationId: string) => Promise<OrganizationInvitation | null | { error: string }> | Fetches a single invitation by ID. |
listInvitations | (organizationId?: string) => Promise<OrganizationInvitation[] | null | { error: string }> | Lists pending invitations for the active or specified organization. |
listUserInvitations | () => Promise<OrganizationInvitation[] | null | { error: string }> | Lists invitations sent to the current user. |
Team Methods
| Key | Type | Description |
|---|---|---|
listTeams | (organizationId?: string) => Promise<OrganizationTeam[] | null | { error: string }> | Lists teams in the active or specified organization. |
createTeam | (name: string, organizationId?: string) => Promise<OrganizationTeam | null | { error: string }> | Creates a new team. |
updateTeam | (teamId: string, data: { name?: string }, organizationId?: string) => Promise<OrganizationTeam | null | { error: string }> | Updates a team's name. |
deleteTeam | (teamId: string, organizationId?: string) => Promise<{ success: boolean } | { error: string }> | Deletes a team. |
setActiveTeam | (teamId: string) => Promise<OrganizationTeam | null | { error: string }> | Sets the active team within the active organization. |
listTeamMembers | (teamId: string) => Promise<OrganizationMember[] | null | { error: string }> | Lists members of a team. |
addTeamMember | (teamId: string, memberId: string, organizationId?: string) => Promise<{ success: boolean } | { error: string }> | Adds an organization member to a team. |
removeTeamMember | (teamId: string, memberId: string) => Promise<{ success: boolean } | { error: string }> | Removes a member from a team. |
listUserTeams | () => Promise<OrganizationTeam[] | null | { error: string }> | Lists teams the current user belongs to across organizations. |
Access Control
| Key | Type | Description |
|---|---|---|
hasPermission | (permissionCheck: PermissionCheck) => boolean | Checks whether the active member has the requested permissions. |
checkRolePermission | (role: string, permissions: Record<string, string[]>) => boolean | Checks whether a given role satisfies the provided permission map. |
getDefaultPermissions | (role: string) => Record<string, string[]> | Returns the layer's default permission map for owner, admin, or member. |
Utilities
| Key | Type | Description |
|---|---|---|
authClient | BetterAuthClient | The underlying Better Auth client instance for direct SDK access. |
Examples
<script setup>
const { organizations, activeOrganization, setActiveOrganization } = useOrganization()
async function switchOrg(slug) {
await setActiveOrganization(null, slug)
}
</script>
<template>
<div>
<p>Active: {{ activeOrganization?.name }}</p>
<USelect
:model-value="activeOrganization?.slug"
:items="organizations.map(o => ({ label: o.name, value: o.slug }))"
@update:model-value="switchOrg"
/>
</div>
</template>
// Invite a member
const { inviteMember } = useOrganization()
await inviteMember('new@example.com', 'member')
// Create a team (requires teams.enabled)
const { createTeam } = useOrganization()
await createTeam('Engineering')
AI Context
composable: useOrganization
package: "@xenterprises/nuxt-x-auth-better"
use-when: >
Organization and team management in a Nuxt app using the nuxt-x-auth-better
layer (Better Auth backend). Provides reactive state and methods for
organizations, members, invitations, teams, and role-based access control.
Requires the Better Auth organization plugin on the server with
teams.enabled when using teams. Pairs with fastify-x-auth-better on the
server side.
