-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
57 lines (48 loc) Β· 1.55 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
53
54
55
56
57
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client/edge";
import { withAccelerate } from "@prisma/extension-accelerate";
export default withAuth(
async function middleware(req) {
const prisma = new PrismaClient().$extends(withAccelerate());
const user = req.nextauth?.token;
const isRequestGoingToDashboard =
req.nextUrl.pathname.startsWith("/api/dashboard");
const isGoingToCloudnary =
req.nextUrl.pathname.startsWith("/api/cloudnary");
// console.log(user)
const profile = await prisma.profile.findUnique({
where: {
email: user?.email!,
},
});
if (user?.isAdmin) {
return NextResponse.next();
}
if ((isRequestGoingToDashboard || isGoingToCloudnary) && user?.isPro) {
return NextResponse.next();
} else if (
(isRequestGoingToDashboard || isGoingToCloudnary) &&
!user?.isPro
) {
if (profile && profile.apiCallCount < 5) {
return NextResponse.next();
} else {
return NextResponse.json(
{ error: "FREE TRIAL LIMIT EXCEEDED" },
{ status: 403 },
);
}
}
},
// {
// callbacks: {
// authorized: ({ token }) => token?.role === 'admin',
// },
// }
);
export const config = {
// matcher: '/((?!api/auth|auth|images|_next/static|_next/image|favicon.ico|^/$).+)',
matcher:
"/((?!api/auth/[^/]+$|auth|api/auth|images|_next/static|_next/image*|contact|favicon.ico|^/$|api/payment/record-payment).+)/",
};