-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
29 lines (24 loc) · 1.06 KB
/
Copy pathmiddleware.ts
File metadata and controls
29 lines (24 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
import { betterFetch } from "@better-fetch/fetch";
import { NextResponse, type NextRequest } from "next/server";
import type { Session } from "better-auth/types";
export default async function authMiddleware(request: NextRequest) {
//This automatically tells TypeScript that the returned data will match the Session shape.
const { data: session } = await betterFetch<Session>(
"/api/auth/get-session", // sending req to the backend to see if user has active session or not.
{
baseURL: request.nextUrl.origin,
headers: {
//get the cookie from the request
cookie: request.headers.get("cookie") || "",
},
},
);
if (!session) { // if no session is found, redirect to sign-in page
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next(); // if session is found, allow the request to proceed
}
//these are the routes that will be protected by the auth middleware
export const config = {
matcher: ["/dashboard"],
};