-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
52 lines (44 loc) · 1.32 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import { NextResponse } from 'next/server';
const isOnboardingRoute = createRouteMatcher(['/onboarding']);
const isPublicRoute = createRouteMatcher([
'/',
'/sign-in(.*)',
'/sign-up(.*)',
'/:username(.*)',
'/sso-callback(.*)',
'/api/og',
'/_next(.*)',
]);
export default clerkMiddleware(async (auth, req) => {
const { userId, sessionClaims } = await auth();
const response = NextResponse.next();
// Add cache headers for post pages
if (req.nextUrl.pathname.match(/\/[^\/]+\/posts(\/.*)?$/)) {
response.headers.set(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59',
);
}
// Rest of the middleware logic...
if (userId && isOnboardingRoute(req)) {
return response;
}
if (!userId && !isPublicRoute(req)) {
const signInUrl = new URL('/sign-in', req.url);
signInUrl.searchParams.set('redirect_url', req.url);
return NextResponse.redirect(signInUrl);
}
if (
userId &&
!sessionClaims?.metadata?.onboardingComplete &&
!isOnboardingRoute(req)
) {
const onboardingUrl = new URL('/onboarding', req.url);
return NextResponse.redirect(onboardingUrl);
}
return response;
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};