-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
28 lines (22 loc) · 803 Bytes
/
proxy.ts
File metadata and controls
28 lines (22 loc) · 803 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
import NextAuth from 'next-auth';
import { NextResponse } from 'next/server';
import { authConfig } from './auth.config';
const { auth } = NextAuth(authConfig);
const protectedPaths = ['/dashboard', '/me', '/reviews'];
export const proxy = auth((req) => {
const isLoggedIn = !!req.auth?.user;
const isProtectedRoute = protectedPaths.some(
(path) =>
req.nextUrl.pathname === path ||
req.nextUrl.pathname.startsWith(`${path}/`),
);
if (!isLoggedIn && isProtectedRoute) {
const loginUrl = new URL('/login', req.nextUrl.origin);
loginUrl.searchParams.set('callbackUrl', req.nextUrl.href);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
});
export const proxyConfig = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};