Skip to content

Commit 934be25

Browse files
authored
fix: Update force-organizations guide to use auth from middleware correctly (#949)
1 parent 2ae40b8 commit 934be25

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

docs/guides/force-organizations.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ In the example below, the `clerkMiddleware()` helper is used to redirect signed
250250
import { clerkMiddleware } from '@clerk/nextjs/server';
251251

252252
export default clerkMiddleware((auth, req) => {
253+
const { userId, orgId } = auth();
254+
253255
// Redirect signed in users to organization selection page if they are not active in an organization
254-
if(auth.userId && !auth.orgId && req.nextUrl.pathname !== "/org-selection"){
256+
if(userId && !orgId && req.nextUrl.pathname !== "/org-selection"){
255257
const searchParams = new URLSearchParams({redirectUrl: req.url});
256258

257259
const orgSelection = new URL(`/org-selection?${searchParams.toString()}`, req.url);
@@ -271,10 +273,12 @@ To limit access to a specific part of the application, you can check if the path
271273
import { clerkMiddleware } from '@clerk/nextjs/server';
272274

273275
export default clerkMiddleware((auth, req) => {
276+
const { userId, orgId } = auth();
277+
274278
// Redirect signed in users to organization selection page if they are not active in an organization
275279
if(
276-
auth.userId &&
277-
!auth.orgId &&
280+
userId &&
281+
!orgId &&
278282
req.nextUrl.pathname.startsWith('/dashboard') &&
279283
req.nextUrl.pathname !== "/dashboard/org-selection"
280284
){

docs/references/nextjs/clerk-middleware.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,16 @@ const isProtectedRoute = createRouteMatcher([
143143
]);
144144

145145
export default clerkMiddleware((auth, req) => {
146+
const { has, redirectToSignIn } = auth()
146147
// Restrict admin routes to users with specific permissions
147148
if (isProtectedRoute(req) &&
148-
!auth().has({ permission: 'org:sys_memberships:manage' }) ||
149-
!auth().has({ permission: 'org:sys_domains_manage' })
149+
!has({ permission: 'org:sys_memberships:manage' }) ||
150+
!has({ permission: 'org:sys_domains_manage' })
150151
){
151152

152153
// Add logic to run if the user does not have the required permissions
153154

154-
return auth.redirectToSignIn();
155+
return redirectToSignIn();
155156
}
156157
});
157158

0 commit comments

Comments
 (0)