-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
31 lines (28 loc) · 1.09 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
// Ref: https://stackoverflow.com/questions/67560587/how-to-protect-routes-in-next-js-next-auth
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
const anonymousRoutes = ["/signin", "/signup", "/contact", "/legal"];
const publicFileRegex = /\.(.*)$/;
export default withAuth(
async function middleware(req) {
const response = NextResponse.next();
return response;
},
{
callbacks: {
authorized: ({ req }) => {
const { pathname } = req.nextUrl;
return Boolean(
req.cookies.get("next-auth.session-token") ||
pathname === "/" || // exclude home page
pathname.startsWith("/_next") || // exclude Next.js internals
pathname.startsWith("/api") || // exclude all API routes
pathname.startsWith("/static") || // exclude static files
publicFileRegex.test(pathname) || // exclude all files in the public folder
anonymousRoutes.some((path) => pathname.startsWith(path))
);
},
},
pages: { signIn: "/signin", error: "/signin" },
}
);