Skip to content

Organization components and hooks

Organizations are on per application (see Organizations). The three components below render nothing while the user is signed out, while the session is still pending a task, and while organizations are off for the instance. All of them come from @zautha/react and @zautha/nextjs; with plain JavaScript use the mount… and open… methods.

The active organization (or the personal account) with a menu: the active one with the user’s role and “Manage organization”, the user’s other organizations, the personal account to switch to, pending invitations to join and “Create organization” while users may create them.

import { OrganizationSwitcher } from "@zautha/react";
export function Header() {
return (
<OrganizationSwitcher
afterSelectOrganizationUrl="/orgs/:slug"
afterSelectPersonalUrl="/account"
afterCreateOrganizationUrl="/orgs/:slug/welcome"
/>
);
}
Prop Meaning
afterSelectOrganizationUrl Where to go after choosing (or joining) an organization; :id and :slug stand for its.
afterSelectPersonalUrl Where to go after choosing the personal account.
afterCreateOrganizationUrl Passed to the CreateOrganization modal; :id and :slug stand for the new organization’s.
afterLeaveOrganizationUrl Passed to the OrganizationProfile modal.
hidePersonal Leave the personal account out of the menu (it is left out anyway while the instance requires an organization).
defaultOpen Open the menu at first.
createOrganizationUrl The page with CreateOrganization (e.g. the hosted login’s /create-organization). Without it, a modal.
organizationProfileUrl The page with OrganizationProfile (e.g. the hosted login’s /organization); :id and :slug stand for the active one’s. Without it, a modal.
organizationProfileProps Props of the OrganizationProfile modal.
createOrganizationProps Props of the CreateOrganization modal.
onError Called with the ZauthaError of an action that failed.

The session’s active organization: a General tab (name and slug, leaving, deleting), a Members tab and an Invitations tab. What is shown follows the user’s membership: each tab and action needs its permission (org:sys_memberships:read for the members, org:sys_memberships:manage for the invitations). To show another organization’s profile, make it active first with setActive.

import { OrganizationProfile } from "@zautha/react";
export function OrganizationPage() {
return <OrganizationProfile afterLeaveOrganizationUrl="/" onEvent={(event) => console.log(event.type)} />;
}
Prop Meaning
afterLeaveOrganizationUrl Where to go after leaving or deleting the organization. Without it the component goes on with the session’s active organization, if any (a modal closes).
onError Called with the ZauthaError of an action that failed.
onEvent Receives organization.left and organization.deleted.

Creates an organization and makes it the session’s active one, then (unless skipped, or the organization holds one member only) offers to invite members. It says so when users may not create organizations.

import { CreateOrganization } from "@zautha/react";
export function NewOrganizationPage() {
return <CreateOrganization afterCreateOrganizationUrl="/orgs/:slug" skipInvitationScreen />;
}
Prop Meaning
afterCreateOrganizationUrl Where to go once done; :id and :slug stand for the new organization’s. Without it the component says it is done (a modal closes).
skipInvitationScreen Leave out the step that invites members.
onError Called with the ZauthaError of an action that failed.

The session’s active organization and the user’s membership of it, from the session token and the user: no request is made until you ask for a list.

import { useOrganization } from "@zautha/react";
export function Members() {
const { isLoaded, organization, membership, memberships } = useOrganization({
memberships: { pageSize: 20 },
invitations: { status: ["pending"] },
});
if (!isLoaded || organization === null || memberships === undefined) return null;
return (
<section>
<h2>
{organization.name} ({membership?.roleName})
</h2>
<ul>
{memberships.data.map((member) => (
<li key={member.id}>
{member.publicUserData.identifier}: {member.roleName}
</li>
))}
</ul>
<button disabled={!memberships.hasNextPage} onClick={memberships.fetchNext}>
More
</button>
</section>
);
}
Param Meaning
memberships true, or { pageSize?, initialPage?, role? }: the active organization’s members (needs org:sys_memberships:read), filtered by a role key.
invitations true, or { pageSize?, initialPage?, status? }: its invitations; read only for a member with org:sys_memberships:manage (otherwise empty).
Result Meaning
isLoaded Whether Zautha has loaded.
organization The active organization with the member’s actions (update, inviteMember, removeMember, …); null without one.
membership The user’s membership of it: role and every permission of the role, system ones included (to show buttons).
memberships A page of members, only when asked for.
invitations A page of invitations, only when asked for.

A list is a PaginatedResult: data, count, page, pageCount, isLoading, isFetching, error, hasNextPage, hasPreviousPage, fetchNext(), fetchPrevious(), fetchPage(page) and revalidate(). A failed read leaves data empty and sets error. membership.permissions is for what to show; your server still decides what is allowed (see Organizations: roles and permissions).

Creating an organization, choosing the active one, and on request the user’s memberships and the invitations to the user’s verified addresses.

import { useOrganizationList } from "@zautha/react";
export function Organizations() {
const { isLoaded, setActive, createOrganization, userMemberships, userInvitations } = useOrganizationList({
userMemberships: true,
userInvitations: { status: ["pending"] },
});
if (!isLoaded || userMemberships === undefined) return null;
return (
<>
<ul>
{userMemberships.data.map((m) => (
<li key={m.id}>
<button onClick={() => void setActive({ organization: m.organization.id })}>{m.organization.name}</button>
</li>
))}
</ul>
{userInvitations?.data.map((invitation) => (
<button key={invitation.id} onClick={() => void invitation.accept()}>
Join
</button>
))}
<button onClick={() => void createOrganization({ name: "Acme" }).then((o) => setActive({ organization: o.id }))}>
New
</button>
</>
);
}

createOrganization does not make the new organization active; setActive does that (an id, a slug while the instance has slugs on, or null for the personal account). userMemberships is true or { pageSize?, initialPage? }, newest first; userInvitations is true or { pageSize?, initialPage?, status? } and by default lists the pending ones. Accepting an invitation (accept()) makes the organization the active one.

@zautha/js mounts the same components into an element, or opens them in a modal:

import type { Zautha } from "@zautha/js";
declare const zautha: Zautha;
declare const element: HTMLElement;
zautha.mountOrganizationSwitcher(element, { hidePersonal: true });
zautha.mountOrganizationProfile(element, { afterLeaveOrganizationUrl: "/" });
zautha.mountCreateOrganization(element, { skipInvitationScreen: true });
// Modals (there is none for the switcher, which is a menu):
zautha.openCreateOrganization({ afterCreateOrganizationUrl: "/orgs/:slug" });
zautha.closeCreateOrganization();
zautha.openOrganizationProfile();
zautha.closeOrganizationProfile();
zautha.unmount(element);

updateMounted(element, props) changes the props of a mounted component without mounting it again; the React components do that when their props change.