-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
31 lines (24 loc) · 863 Bytes
/
Copy pathproxy.js
File metadata and controls
31 lines (24 loc) · 863 Bytes
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
import { getToken } from 'next-auth/jwt';
import { NextResponse } from 'next/server';
export async function proxy(request) {
const { pathname } = request.nextUrl;
const publicPaths = ['/login', '/register', '/landing'];
const isPublic =
publicPaths.some((p) => pathname.startsWith(p)) ||
pathname.startsWith('/api/auth') ||
pathname.startsWith('/_next') ||
pathname.startsWith('/favicon');
if (isPublic) {
return NextResponse.next();
}
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET });
if (!token) {
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api/auth|_next/static|_next/image|favicon.ico).*)'],
};