-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (29 loc) · 1012 Bytes
/
middleware.ts
File metadata and controls
37 lines (29 loc) · 1012 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
25
26
27
28
29
30
31
32
33
34
35
36
37
import { betterFetch } from "@better-fetch/fetch";
import type { auth } from "@/lib/auth";
import { NextResponse, type NextRequest } from "next/server";
type Session = typeof auth.$Infer.Session;
export async function middleware(request: NextRequest) {
const { data: session } = await betterFetch<Session>("/api/auth/get-session", {
baseURL: request.nextUrl.origin,
headers: {
cookie: request.headers.get("cookie") || "",
},
});
const isAuthPage =
request.nextUrl.pathname === "/signin" ||
request.nextUrl.pathname === "/signup";
const isDashboardPage = request.nextUrl.pathname.startsWith("/dashboard");
if (!session) {
if (isDashboardPage) {
return NextResponse.redirect(new URL("/signin", request.url));
}
return NextResponse.next();
}
if (isAuthPage) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/signin", "/signup"],
};