-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
42 lines (31 loc) · 1.13 KB
/
proxy.ts
File metadata and controls
42 lines (31 loc) · 1.13 KB
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { prisma } from './lib/prisma';
import { auth } from './lib/auth';
const protectedPaths = ["/profile","profile/edit","/collections","/question"];
const verifyPath = (pathname : string) => {
return (protectedPaths.some(e => e === pathname) ) ? true : false;
}
export async function proxy(request: NextRequest) {
const session = await auth();
if(session && request.nextUrl.pathname === "/login")
return NextResponse.redirect(new URL("/",request.url ));
const isProtected = verifyPath(request.nextUrl.pathname);
if(!session) return (isProtected)
? NextResponse.redirect(new URL("/",request.url ))
: NextResponse.next();
const userFound = await prisma.user.findUnique({
where: { id: session.user.id },
select: { bannedAt: true }
});
if (userFound?.bannedAt) {
return NextResponse.json(
{ message: 'You have been banned!' },
{ status: 403 }
);
}
return NextResponse.next();
}
export const config = {
matcher: '/:path*',
};