-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
30 lines (25 loc) · 782 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
30 lines (25 loc) · 782 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
const publicPaths = ["/", "/login", "/signup", "/unauthenticated"];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (publicPaths.includes(pathname)) {
return NextResponse.next();
}
const token = await getToken({ req: request, secret: process.env.AUTH_SECRET });
if (!token) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/item-view/:path*",
"/form-view/:path*",
"/create-item/:path*",
"/update-item/:path*",
"/delete-item/:path*",
"/api/items/:path*",
],
};