mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
fix: remove nextjs auth middleware and provide a guide instead (#346)
This commit is contained in:
@@ -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"],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user