-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
34 lines (28 loc) · 935 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
32
33
34
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { auth } from "./lib/auth";
import { headers } from "next/headers";
const publicRoutes = ["/login", "/signup"];
const protectedRoutes = [
"/",
"/transactions",
"/budgets",
"/pots",
"/recurring-bills",
];
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const isProtectedRoute = protectedRoutes.includes(path);
const isPublicRoute = publicRoutes.includes(path);
const sessionData = await auth.api.getSession({
headers: await headers(),
});
const session = sessionData?.session;
if (isProtectedRoute && !session?.id) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (isPublicRoute && session?.id) {
return NextResponse.redirect(new URL("/", request.url));
}
}