Skip to content

Commit 398737f

Browse files
authored
docs: Update Clerk integration example to Clerk Core 2 (#1021 by @gcascio)
1 parent 0d52803 commit 398737f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Diff for: docs/pages/docs/routing/middleware.mdx

+9-8
Original file line numberDiff line numberDiff line change
@@ -592,24 +592,25 @@ Note that if you use a [`localePrefix`](#locale-prefix) other than `always`, you
592592

593593
### Example: Integrating with Clerk
594594

595-
[`@clerk/nextjs`](https://clerk.com/docs/references/nextjs/overview) provides a middleware that can be integrated with `next-intl` by using the [`beforeAuth` hook](https://clerk.com/docs/references/nextjs/auth-middleware#using-before-auth-to-execute-middleware-before-authentication). By doing this, the middleware from `next-intl` will run first, potentially redirect or rewrite incoming requests, followed by the middleware from `@clerk/next` acting on the response.
595+
[`@clerk/nextjs`](https://clerk.com/docs/references/nextjs/overview) provides a middleware that can be [combined](https://clerk.com/docs/references/nextjs/clerk-middleware#combine-middleware) with other middlewares like the one provided by `next-intl`. By combining them, the middleware from `@clerk/next` will first ensure protected routes are handled appropriately. Subsequently, the middleware from `next-intl` will run, potentially redirecting or rewriting incoming requests.
596596

597597
```tsx filename="middleware.ts"
598-
import {authMiddleware} from '@clerk/nextjs';
598+
import {clerkMiddleware, createRouteMatcher} from '@clerk/nextjs/server';
599599
import createMiddleware from 'next-intl/middleware';
600600

601601
const intlMiddleware = createMiddleware({
602602
locales: ['en', 'de'],
603603
defaultLocale: 'en'
604604
});
605605

606-
export default authMiddleware({
607-
beforeAuth(request) {
608-
return intlMiddleware(request);
609-
},
606+
const isProtectedRoute = createRouteMatcher([
607+
'/:locale/dashboard(.*)'
608+
]);
609+
610+
export default clerkMiddleware((auth, req) => {
611+
if (isProtectedRoute(req)) auth().protect();
610612

611-
// Ensure that locale-specific sign in pages are public
612-
publicRoutes: ['/:locale', '/:locale/sign-in']
613+
return intlMiddleware(req);
613614
});
614615

615616
export const config = {

0 commit comments

Comments
 (0)