fix: remove nextjs auth middleware and provide a guide instead (#346)

This commit is contained in:
Bereket Engida
2024-10-26 16:46:08 +03:00
committed by GitHub
parent 8a751b8524
commit d4c4c0b9e9
3 changed files with 39 additions and 103 deletions

View File

@@ -1,17 +1,22 @@
import { authMiddleware } from "better-auth/next-js";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { client } from "@/lib/auth-client";
import { NextRequest, NextResponse } from "next/server";
export default authMiddleware({
customRedirect: async (session, request) => {
const baseURL = request.nextUrl.origin;
if (request.nextUrl.pathname === "/dashboard" && !session) {
return NextResponse.redirect(new URL("/sign-in", baseURL));
}
return NextResponse.next();
},
});
export default async function authMiddleware(request: NextRequest) {
const { data: session } = await client.getSession({
fetchOptions: {
headers: {
//get the cookie from the request
cookie: request.headers.get("cookie") || "",
},
},
});
if (!session) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard", "/sign-in"],
matcher: ["/dashboard"],
};