Skip to content

Commit e9d50b8

Browse files
authored
chore(react-router): Accept organizationSyncOptions option in middleware (#6927)
1 parent 6dfb3ec commit e9d50b8

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

.changeset/fresh-waves-sneeze.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
"@clerk/react-router": minor
3+
---
4+
5+
Added `organizationSyncOptions` option to `clerkMiddleware()`. It's used to activate a specific organization or personal account based on URL path parameters.
6+
7+
Usage:
8+
9+
```ts
10+
// app/root.tsx
11+
export const middleware: Route.MiddlewareFunction[] = [
12+
clerkMiddleware({
13+
organizationSyncOptions: {
14+
organizationPatterns: [
15+
'/orgs/:slug', // Match the org slug
16+
'/orgs/:slug/(.*)', // Wildcard match for optional trailing path segments
17+
],
18+
}
19+
})
20+
]
21+
```
22+
23+
To learn more about best practices for using organization slugs to manage the active organization, check out this [guide](https://clerk.com/docs/organizations/org-slugs-in-urls).

packages/react-router/src/server/clerkMiddleware.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export const clerkMiddleware = (options?: ClerkMiddlewareOptions): MiddlewareFun
3636
const loadedOptions = loadOptions(args, options);
3737
const { audience, authorizedParties } = loadedOptions;
3838
const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = loadedOptions;
39+
const { organizationSyncOptions } = loadedOptions;
3940
const requestState = await clerkClient(args).authenticateRequest(clerkRequest, {
41+
organizationSyncOptions,
4042
audience,
4143
authorizedParties,
4244
signInUrl,

packages/react-router/src/server/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Organization, Session, User, VerifyTokenOptions } from '@clerk/backend';
2-
import type { RequestState, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';
2+
import type {
3+
OrganizationSyncOptions,
4+
RequestState,
5+
SignedInAuthObject,
6+
SignedOutAuthObject,
7+
} from '@clerk/backend/internal';
38
import type {
49
LegacyRedirectProps,
510
MultiDomainAndOrProxy,
@@ -31,6 +36,12 @@ export type ClerkMiddlewareOptions = {
3136
machineSecretKey?: string;
3237
signInUrl?: string;
3338
signUpUrl?: string;
39+
/**
40+
* Used to activate a specific [organization](https://clerk.com/docs/guides/organizations/overview) or [personal account](https://clerk.com/docs/guides/dashboard/overview) based on URL path parameters. If there's a mismatch between the active organization in the session (e.g., as reported by `auth()`) and the organization indicated by the URL, an attempt to activate the organization specified in the URL will be made.
41+
*
42+
* If the activation can't be performed, either because an organization doesn't exist or the user lacks access, the active organization in the session won't be changed. Ultimately, it's the responsibility of the page to verify that the resources are appropriate to render given the URL and handle mismatches appropriately (e.g., by returning a 404).
43+
*/
44+
organizationSyncOptions?: OrganizationSyncOptions;
3445
} & Pick<VerifyTokenOptions, 'audience' | 'authorizedParties'> &
3546
MultiDomainAndOrProxy &
3647
SignInForceRedirectUrl &

0 commit comments

Comments
 (0)