mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 12:27:44 +00:00
demo: change next middleware
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
import { authMiddleware } from "better-auth/next-js";
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import type { Session } from "./lib/auth-types";
|
||||
|
||||
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 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) {
|
||||
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