-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
31 lines (29 loc) · 1012 Bytes
/
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
import { routeIsHomeOrArticle, routeIsLoginOrSignup } from "@/lib/utils";
import { NextRequestWithAuth, withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
async function middleware(request: NextRequestWithAuth) {
const pathname = request.nextUrl.pathname;
const session = request.nextauth.token;
if (session && routeIsLoginOrSignup(pathname)) {
return NextResponse.redirect(new URL("/home", request.url));
}
},
{
callbacks: {
authorized({ req, token }) {
if (token) return true;
if (
routeIsLoginOrSignup(req.nextUrl.pathname) ||
routeIsHomeOrArticle(req.nextUrl.pathname)
)
return true;
return false;
},
},
}
);
export const config = {
matcher:
"/((?!api|_next/static|_next/image|gradient|favicon.ico|articles|categories|comments|users).*)", // Matches all routes except api routes (Route Handlers), static files, and image files
};