#How can i do server-side redirecting using middleware.ts if i already have a middleware.ts file

4 messages · Page 1 of 1 (latest)

sacred idol
#

Hi, i have a nextjs app and the auth is managed by clerk. That means i already have a middleware.ts file with some code provided by clerk (the code is shown in the 1-st image).

My questions is: I read in nextjs docs that i can have just one middlewate.ts file per project, but how i can integrate the middleware from image (2-nd image).

ember sableBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

sacred idol
#

How can i do server-side redirecting using middleware.ts if i already have a middleware.ts file

mortal dawn
#

You compose/combine Clerks middleware by returning the second milddwarefrom from clerkMiddleware()

import {
  clerkMiddleware,
  createRouteMatcher,
  redirectToSignIn,
} from '@clerk/nextjs/server';
import createMiddleware from 'next-intl/middleware';

import { AppConfig } from './utils/AppConfig';

const intlMiddleware = createMiddleware({
  locales: AppConfig.locales,
  localePrefix: AppConfig.localePrefix,
  defaultLocale: AppConfig.defaultLocale,
});

const isProtectedRoute = createRouteMatcher([
  'dashboard/(.*)',
]);

export default clerkMiddleware((auth, req) => {
  if (isProtectedRoute(req)) auth().protect();

  return intlMiddleware(req);
});

export const config = {
  matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};

https://clerk.com/docs/references/nextjs/clerk-middleware#combine-middleware

The clerkMiddleware() function allows you to protect your Next.js application using Middleware.