You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Learn how to add organizations to your Next.js app with Clerk's prebuilt components.
4
-
sdk: nextjs
2
+
title: Organizations quickstart
3
+
description: Learn how to add organizations to your application with Clerk's prebuilt components.
5
4
---
6
5
7
6
Organizations let you group users with roles and permissions, enabling you to build multi-tenant B2B apps like Slack (workspaces), Linear (teams), or Vercel (projects) where users can switch between different team contexts.
8
7
9
-
> [!NOTE]
10
-
> Just getting started with Clerk and Next.js? See the [quickstart tutorial](/docs/nextjs/getting-started/quickstart)!
11
-
12
8
<Steps>
13
-
## Enable organizations in the Clerk Dashboard
9
+
## Enable organizations
14
10
15
11
Organizations are disabled by default. To enable them:
16
12
@@ -23,13 +19,11 @@ Organizations let you group users with roles and permissions, enabling you to bu
23
19
24
20
Learn more about configuring organizations in the [dedicated guide](/docs/guides/organizations/configure).
25
21
26
-
## Add the OrganizationSwitcher component
22
+
## Add the `<OrganizationSwitcher />` component
27
23
28
-
The [`<OrganizationSwitcher />`](/docs/reference/components/organization/organization-switcher) component is the easiest way to let users create, switch between, and manage organizations. Add it to your app's header or navigation:
24
+
The [`<OrganizationSwitcher />`](/docs/reference/components/organization/organization-switcher) component is the easiest way to let users create, switch between, and manage organizations. It's recommended to add it in your app's header or navigation so that it's always readily available to your users. For example:
Visit your app locally at [localhost:3000](http://localhost:3000).
93
-
94
-
## Create and switch organization
95
-
96
-
Use `<OrganizationSwitcher />` to create, switch between, and manage organizations.
97
-
98
-
1. Click on the `<OrganizationSwitcher />` component, then **Create an organization**.
99
-
100
-
1. Enter `Acme Org` as the organization name.
101
-
102
-
1. Invite users to your organization and select their role.
103
-
104
-
## Protect routes by organization and roles
105
-
106
-
You can protect routes based on organization membership, roles, and permissions. The `/protected` page checks if you are an admin in the `Acme Corp` organization:
// Check if organization name matches (e.g., "Acme Corp")
129
-
const requiredOrgName ='Acme Corp'
130
-
if (organization.name!==requiredOrgName) {
131
-
return (
132
-
<pclassName="text-red-600">
133
-
You are currently not signed in as an <strong>admin</strong> in the{''}
134
-
<strong>{requiredOrgName}</strong> organization.
135
-
</p>
136
-
)
137
-
}
138
-
139
-
return (
140
-
<pclassName="text-green-600">
141
-
You are currently signed in as an <strong>admin</strong> in the{''}
142
-
<strong>{requiredOrgName}</strong> organization.
143
-
</p>
144
-
)
145
-
}
146
-
```
147
-
148
-
Learn more about protecting routes and checking organization roles in the [authorization guide](/docs/guides/organizations/roles-and-permissions).
38
+
<Tabsitems={['Client-side', 'Server-side']}>
39
+
<Tab>
40
+
Use Clerk's [`useOrganization()`](/docs/reference/hooks/use-organization) hook to access information about the currently active organization. Use Clerk's [`useOrganizationList()`](/docs/reference/hooks/use-organization-list) hook to access information about the current user's organization memberships.
41
+
42
+
```tsx
43
+
exportdefaultfunction Page() {
44
+
const { organization } =useOrganization()
45
+
const { userMemberships } =useOrganizationList({
46
+
userMemberships: true,
47
+
})
48
+
49
+
return (
50
+
<divclassName="p-8">
51
+
<h1className="text-2xl font-bold mb-4">
52
+
Welcome to the <strong>{organization?.name}</strong> organization
53
+
</h1>
54
+
<pclassName="mb-6">
55
+
Your role in this organization:{''}
56
+
<strong>
57
+
{/* Find the organization membership that matches the
58
+
currently active organization and return the role */}
[Clerk's JS Backend SDK](/docs/js-backend/getting-started/quickstart) provides methods for accessing organization data server-side. For example, to get information about an organization, you can use the [`getOrganization()`](/docs/reference/backend/organization/get-organization) method. It requires the organization ID, which can be accessed through the [`Auth`](/docs/reference/backend/types/auth-object) object.
74
+
75
+
This example is written for Next.js App Router, but can be adapted to other frameworks by using the appropriate method for accessing the [`Auth` object](/docs/reference/backend/types/auth-object), and the appropriate initialization for `clerkClient()`.
You can protect content and even entire routes based on organization membership, roles, and permissions by performing [authorization checks](!authorization-check).
116
+
117
+
In the following example, the page is protected from unauthenticated users, users that don't have the `org:admin` role, and users that are not in the `Acme Corp` organization.
118
+
119
+
- The [`Auth`](/docs/reference/backend/types/auth-object) object is used to access the `isSignedIn` and `has()` properties.
120
+
- The `isSignedIn` property is used to check if the user is signed in.
121
+
- The `has()` helper is used to check if the user has the `org:admin` role.
122
+
- The [`useOrganization()`](/docs/reference/hooks/use-organization) hook is used to access the organization data.
123
+
- The organization name is checked to ensure it matches the required organization name. If a user is not in the required organization, the page will display a message and the [`<OrganizationSwitcher />`](/docs/reference/components/organization/organization-switcher) component will be rendered to allow the user to switch to the required organization.
For more examples on how to perform authorization checks, see the [dedicated guide](/docs/guides/secure/authorization-checks).
178
+
</Tab>
179
+
180
+
<Tab>
181
+
You can protect content and even entire routes based on organization membership, roles, and permissions by performing [authorization checks](!authorization-check).
182
+
183
+
In the following example, the page is protected from unauthenticated users, users that don't have the `org:admin` role, and users that are not in the `Acme Corp` organization.
184
+
185
+
- The [`Auth`](/docs/reference/backend/types/auth-object) object is used to access the `isAuthenticated`, `orgId`, and `has()` properties.
186
+
- The `isAuthenticated` property is used to check if the user is authenticated.
187
+
- The `orgId` property is used to check if there is an [active organization](!active-organization).
188
+
- The `has()` helper is used to check if the user has the `org:admin` role.
189
+
- To fetch the organization server-side, the [`clerkClient()`](/docs/reference/nextjs/overview#clerk-client) helper is used to access the [`getOrganization()`](/docs/reference/backend/organization/get-organization) method.
190
+
- The organization name is checked to ensure it matches the required organization name. If a user is not in the required organization, the page will display a message and the [`<OrganizationSwitcher />`](/docs/reference/components/organization/organization-switcher) component will be rendered to allow the user to switch to the required organization.
191
+
192
+
This example is written for Next.js App Router, but can be adapted to other frameworks by using the appropriate method for accessing the [`Auth` object](/docs/reference/backend/types/auth-object).
For more examples on how to perform authorization checks, see the [dedicated guide](/docs/guides/secure/authorization-checks).
256
+
</Tab>
257
+
</Tabs>
149
258
150
259
## It's time to build your B2B SaaS!
151
260
152
261
You've added Clerk organizations to your Next.js app 🎉. Ready to scale to enterprise customers?
153
262
154
-
-**Control access** with [custom roles and permissions](/docs/guides/organizations/roles-and-permissions) define granular permissions for different user types within organizations.
263
+
-**Control access** with [custom roles and permissions](/docs/guides/organizations/roles-and-permissions): define granular permissions for different user types within organizations.
155
264
156
-
-**Onboard entire companies** with [verified domains](/docs/guides/organizations/verified-domains) automatically invite users with approved email domains (e.g., `@company.com`) to join organizations without manual invitations.
265
+
-**Onboard entire companies** with [verified domains](/docs/guides/organizations/verified-domains): automatically invite users with approved email domains (e.g., `@company.com`) to join organizations without manual invitations.
157
266
158
-
-**Enable enterprise SSO** with [SAML and OIDC](/docs/guides/organizations/sso) let customers authenticate through their identity provider (Okta, Entra ID, Google Workspace) with unlimited connections, no per-connection fees.
267
+
-**Enable enterprise SSO** with [SAML and OIDC](/docs/guides/organizations/sso): let customers authenticate through their identity provider (Okta, Entra ID, Google Workspace) with unlimited connections, no per-connection fees.
159
268
</Steps>
160
269
161
-
## Next steps
270
+
## Additional resources
162
271
163
272
<Cards>
164
273
-[Create and manage organizations](/docs/guides/organizations/create-and-manage)
0 commit comments