-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
24 lines (15 loc) · 745 Bytes
/
middleware.ts
File metadata and controls
24 lines (15 loc) · 745 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
import { NextResponse, type NextRequest } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
const PROTECTED_ROUTES = ["/dashboard", "/upload", "/editor", "/account", "/project"];
export default async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const isProtected = PROTECTED_ROUTES.some((route) => path.startsWith(route));
const session = getSessionCookie(request);
if (isProtected && !session) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
if (session && ["/sign-in", "/sign-up", "/reset-password", "/forgot-password"].includes(path)) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}