mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 12:27:43 +00:00
feat: add cookie helper for middlewares (#1275)
This commit is contained in:
@@ -1,20 +1,9 @@
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import type { Session } from "./lib/auth-types";
|
||||
import { getSessionCookie } from "better-auth";
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const { data: session } = await betterFetch<Session>(
|
||||
"/api/auth/get-session",
|
||||
{
|
||||
baseURL: request.nextUrl.origin,
|
||||
headers: {
|
||||
//get the cookie from the request
|
||||
cookie: request.headers.get("cookie") || "",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!session) {
|
||||
const cookies = getSessionCookie(request);
|
||||
if (!cookies) {
|
||||
return NextResponse.redirect(new URL("/", request.url));
|
||||
}
|
||||
return NextResponse.next();
|
||||
|
||||
Reference in New Issue
Block a user