mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-11 04:19:31 +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));
|
||||
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"],
|
||||
};
|
||||
|
||||
@@ -246,7 +246,7 @@
|
||||
"@noble/hashes": "^1.5.0",
|
||||
"@simplewebauthn/browser": "^10.0.0",
|
||||
"@simplewebauthn/server": "^10.0.1",
|
||||
"better-call": "0.2.12",
|
||||
"better-call": "0.2.13",
|
||||
"consola": "^3.2.3",
|
||||
"defu": "^6.1.4",
|
||||
"jose": "^5.9.4",
|
||||
|
||||
@@ -168,6 +168,7 @@ export function getEndpoints<
|
||||
if (!afterPlugins?.length) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
let response = new Response(JSON.stringify(e.body), {
|
||||
status: statusCode[e.status],
|
||||
headers: e.headers,
|
||||
|
||||
12
pnpm-lock.yaml
generated
12
pnpm-lock.yaml
generated
@@ -1349,8 +1349,8 @@ importers:
|
||||
specifier: ^10.0.1
|
||||
version: 10.0.1(encoding@0.1.13)
|
||||
better-call:
|
||||
specifier: 0.2.12
|
||||
version: 0.2.12
|
||||
specifier: 0.2.13
|
||||
version: 0.2.13
|
||||
consola:
|
||||
specifier: ^3.2.3
|
||||
version: 3.2.3
|
||||
@@ -7181,8 +7181,8 @@ packages:
|
||||
resolution: {integrity: sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
better-call@0.2.12:
|
||||
resolution: {integrity: sha512-xuR4hJEtirZYI4UXOEVKaipghWcxxo4/tW3FUTIXIIG1X9nb2V/VH4HZP7fuD7ONHoNntwtPIurDNeK/SswxgA==}
|
||||
better-call@0.2.13:
|
||||
resolution: {integrity: sha512-Y1rOwUtTYeH4usW7XNqonoo+Dtcb2sXL6m6zcLsgTYSD2ANqyELxkeA8BkCTWonN1y+Pt9CMbcn5vILaPohdUg==}
|
||||
|
||||
better-call@0.2.3-beta.2:
|
||||
resolution: {integrity: sha512-ybOtGcR4pOsHI2XE+urR9zcmK+s0YnhJSx8KDj6ul7MUEyYOiMEnq/bylyH62/7qXuYb9q8Oqkp9NF9vWOZ4Mg==}
|
||||
@@ -10369,10 +10369,12 @@ packages:
|
||||
|
||||
libsql@0.3.19:
|
||||
resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==}
|
||||
cpu: [x64, arm64, wasm32]
|
||||
os: [darwin, linux, win32]
|
||||
|
||||
libsql@0.4.5:
|
||||
resolution: {integrity: sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==}
|
||||
cpu: [x64, arm64, wasm32]
|
||||
os: [darwin, linux, win32]
|
||||
|
||||
lilconfig@2.1.0:
|
||||
@@ -23491,7 +23493,7 @@ snapshots:
|
||||
dependencies:
|
||||
safe-buffer: 5.1.2
|
||||
|
||||
better-call@0.2.12:
|
||||
better-call@0.2.13:
|
||||
dependencies:
|
||||
'@better-fetch/fetch': 1.1.12
|
||||
rou3: 0.5.1
|
||||
|
||||
Reference in New Issue
Block a user