import { betterFetch } from "@better-fetch/fetch"; import type { Session } from "better-auth/types"; import { type NextRequest, NextResponse } from "next/server"; export default async function authMiddleware(request: NextRequest) { const protocol = request.headers.get("x-forwarded-proto") || "http"; const host = request.headers.get("host") || "localhost:3000"; console.log(protocol) console.log(host) try { const { data: session } = await betterFetch( "http://localhost:3000/api/auth/get-session", { baseURL: `${protocol}://${host}`, headers: { //get the cookie from the request cookie: request.headers.get("cookie") || "", host: host }, }, ); if (!session) { return NextResponse.redirect(new URL("/login", request.url)); } return NextResponse.next(); } catch (error) { console.log("Middlewaree", error); return NextResponse.redirect(new URL("/login", request.url)); } } export const config = { matcher: ["/devices", "/", "/payments", "/payments/:paymentId"], };