General
Organizations

Active Organization

Learn how to access the active organization.

In all organization-related functionality you need to access the active organization. The active organization is identified by the slug in the URL. We made it as simple as possible to get the active organization, i.e. we use a middleware to pass the slug as header for all organization routes, so you don't have to pass the slug.

Server Component

To access the active organization in a server component:

apps/dashboard/app/organizations/[slug]/(organization)/example/page.tsx
import { getAuthOrganizationContext } from '@workspace/auth/context';

export default async function ServerComponent() {
  const ctx = await getAuthOrganizationContext();
  const activeOrganization = ctx.organization;

  return <>{activeOrganization.name}</>;
}

Client Component

To access the active organization in a client component:

apps/dashboard/components/organizations/slug/example.tsx
'use client';

import { useActiveOrganization } from '~/hooks/use-active-organization';

export function ClientComponent() {
  const activeOrganization = useActiveOrganization();

  return <>{activeOrganization.name}</>;
}

You can call useActiveOrganization as many times as you like - it has no performance implication.

Server Action

Server actions are using next-safe-action and automatically call getAuthOrganizationContext() for you. To access the active organization in a server action:

apps/dashboard/actions/example/my-server-action.ts
import { authOrganizationActionClient } from '~/actions/safe-action';

export const myServerAction = authOrganizationActionClient
  .metadata({ actionName: 'myServerAction' })
  .action(async ({ ctx }) => {
    const activeOrganization = ctx.organization;
  });

Implicit Validation

Auth, slug & membership validation are automatically handled in getAuthOrganizationContext().

Deduplication

You can call getAuthOrganizationContext() as much as you like. It's deduplicated for the current HTTP request. This means it will be executed only once and the result is cached and shared amongst all callers.