-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
41 lines (34 loc) · 1.08 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
import { authMiddleware } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export default authMiddleware({
publicRoutes: ["/"],
afterAuth: async (auth, req) => {
// Allow public routes and non-authenticated users
if (!auth.userId) {
return;
}
// Allow access to the onboarding page
if (req.nextUrl.pathname === '/onboarding') {
return;
}
// Check if trying to access dashboard or other protected routes
if (req.nextUrl.pathname.startsWith('/dashboard')) {
try {
const profile = await prisma.userProfile.findUnique({
where: { userId: auth.userId },
});
// If no profile exists, redirect to onboarding
if (!profile) {
return NextResponse.redirect(new URL('/onboarding', req.url));
}
} catch (error) {
console.error('Error checking profile:', error);
}
}
},
});
export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};