-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmiddleware.ts
More file actions
33 lines (29 loc) · 1.06 KB
/
Copy pathmiddleware.ts
File metadata and controls
33 lines (29 loc) · 1.06 KB
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
32
33
import NextAuth from "next-auth";
import { NextResponse } from "next/server";
import authConfig from "./auth.config";
import { configRoutes } from "./config/routes";
import { createRouteMatchers } from "./lib/route";
const { auth } = NextAuth(authConfig);
export default auth((req) => {
const { isPublicRoute, isProtectedRoute, isApiRoute, isAuthRoute } = createRouteMatchers(configRoutes, req);
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
console.log(`Public: ${isPublicRoute}`);
console.log(`Protected: ${isProtectedRoute}`);
console.log(`Api: ${isApiRoute}`);
console.log(`Auth: ${isAuthRoute}`);
if (isProtectedRoute && !isLoggedIn) {
return NextResponse.redirect(new URL("/auth/login", req.url));
}
// console.log(`Middleware: ${req.nextUrl.pathname}`);
});
export const config = {
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};