mirror of
https://github.com/harivansh-afk/Saas-Teamspace-2.git
synced 2026-04-19 08:01:23 +00:00
initial commit
This commit is contained in:
commit
9963e01acc
158 changed files with 48198 additions and 0 deletions
73
src/middleware.ts
Normal file
73
src/middleware.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
|
||||
import {
|
||||
type NextFetchEvent,
|
||||
type NextRequest,
|
||||
NextResponse,
|
||||
} from 'next/server';
|
||||
import createMiddleware from 'next-intl/middleware';
|
||||
|
||||
import { AllLocales, AppConfig } from './utils/AppConfig';
|
||||
|
||||
const intlMiddleware = createMiddleware({
|
||||
locales: AllLocales,
|
||||
localePrefix: AppConfig.localePrefix,
|
||||
defaultLocale: AppConfig.defaultLocale,
|
||||
});
|
||||
|
||||
const isProtectedRoute = createRouteMatcher([
|
||||
'/dashboard(.*)',
|
||||
'/:locale/dashboard(.*)',
|
||||
'/onboarding(.*)',
|
||||
'/:locale/onboarding(.*)',
|
||||
'/api(.*)',
|
||||
'/:locale/api(.*)',
|
||||
]);
|
||||
|
||||
export default function middleware(
|
||||
request: NextRequest,
|
||||
event: NextFetchEvent,
|
||||
) {
|
||||
if (
|
||||
request.nextUrl.pathname.includes('/sign-in')
|
||||
|| request.nextUrl.pathname.includes('/sign-up')
|
||||
|| isProtectedRoute(request)
|
||||
) {
|
||||
return clerkMiddleware((auth, req) => {
|
||||
const authObj = auth();
|
||||
|
||||
if (isProtectedRoute(req)) {
|
||||
const locale
|
||||
= req.nextUrl.pathname.match(/(\/.*)\/dashboard/)?.at(1) ?? '';
|
||||
|
||||
const signInUrl = new URL(`${locale}/sign-in`, req.url);
|
||||
|
||||
authObj.protect({
|
||||
// `unauthenticatedUrl` is needed to avoid error: "Unable to find `next-intl` locale because the middleware didn't run on this request"
|
||||
unauthenticatedUrl: signInUrl.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
authObj.userId
|
||||
&& !authObj.orgId
|
||||
&& req.nextUrl.pathname.includes('/dashboard')
|
||||
&& !req.nextUrl.pathname.endsWith('/organization-selection')
|
||||
) {
|
||||
const orgSelection = new URL(
|
||||
'/onboarding/organization-selection',
|
||||
req.url,
|
||||
);
|
||||
|
||||
return NextResponse.redirect(orgSelection);
|
||||
}
|
||||
|
||||
return intlMiddleware(req);
|
||||
})(request, event);
|
||||
}
|
||||
|
||||
return intlMiddleware(request);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/((?!.+\\.[\\w]+$|_next|monitoring).*)', '/', '/(api|trpc)(.*)'], // Also exclude tunnelRoute used in Sentry from the matcher
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue