-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
29 lines (22 loc) · 886 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { auth } from "auth";
const protectedRoutes = ["/dashboard", "/modules", "/profile"];
export default async function middleware(request: NextRequest) {
const session = await auth();
const isProtected = protectedRoutes.some((route) =>
request.nextUrl.pathname.startsWith(route),
);
if (!session && isProtected) {
const absoluteURL = new URL("/sign-in", request.nextUrl.origin);
return NextResponse.redirect(absoluteURL.toString());
}
if (session && request.nextUrl.pathname.startsWith("/sign-in")) {
const absoluteURL = new URL("/dashboard", request.nextUrl.origin);
return NextResponse.redirect(absoluteURL.toString());
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};