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 { betterFetch } from "@better-fetch/fetch";
|
||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "@/lib/auth";
|
import type { Session } from "./lib/auth-types";
|
||||||
|
|
||||||
export default authMiddleware({
|
export async function middleware(request: NextRequest) {
|
||||||
customRedirect: async (session, request) => {
|
const { data: session } = await betterFetch<Session>(
|
||||||
const baseURL = request.nextUrl.origin;
|
"/api/auth/get-session",
|
||||||
if (request.nextUrl.pathname === "/dashboard" && !session) {
|
{
|
||||||
return NextResponse.redirect(new URL("/sign-in", baseURL));
|
baseURL: request.nextUrl.origin,
|
||||||
}
|
headers: {
|
||||||
return NextResponse.next();
|
//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 = {
|
export const config = {
|
||||||
matcher: ["/dashboard", "/sign-in"],
|
matcher: ["/dashboard"],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -246,7 +246,7 @@
|
|||||||
"@noble/hashes": "^1.5.0",
|
"@noble/hashes": "^1.5.0",
|
||||||
"@simplewebauthn/browser": "^10.0.0",
|
"@simplewebauthn/browser": "^10.0.0",
|
||||||
"@simplewebauthn/server": "^10.0.1",
|
"@simplewebauthn/server": "^10.0.1",
|
||||||
"better-call": "0.2.12",
|
"better-call": "0.2.13",
|
||||||
"consola": "^3.2.3",
|
"consola": "^3.2.3",
|
||||||
"defu": "^6.1.4",
|
"defu": "^6.1.4",
|
||||||
"jose": "^5.9.4",
|
"jose": "^5.9.4",
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ export function getEndpoints<
|
|||||||
if (!afterPlugins?.length) {
|
if (!afterPlugins?.length) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = new Response(JSON.stringify(e.body), {
|
let response = new Response(JSON.stringify(e.body), {
|
||||||
status: statusCode[e.status],
|
status: statusCode[e.status],
|
||||||
headers: e.headers,
|
headers: e.headers,
|
||||||
|
|||||||
12
pnpm-lock.yaml
generated
12
pnpm-lock.yaml
generated
@@ -1349,8 +1349,8 @@ importers:
|
|||||||
specifier: ^10.0.1
|
specifier: ^10.0.1
|
||||||
version: 10.0.1(encoding@0.1.13)
|
version: 10.0.1(encoding@0.1.13)
|
||||||
better-call:
|
better-call:
|
||||||
specifier: 0.2.12
|
specifier: 0.2.13
|
||||||
version: 0.2.12
|
version: 0.2.13
|
||||||
consola:
|
consola:
|
||||||
specifier: ^3.2.3
|
specifier: ^3.2.3
|
||||||
version: 3.2.3
|
version: 3.2.3
|
||||||
@@ -7181,8 +7181,8 @@ packages:
|
|||||||
resolution: {integrity: sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==}
|
resolution: {integrity: sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==}
|
||||||
engines: {node: '>= 0.8'}
|
engines: {node: '>= 0.8'}
|
||||||
|
|
||||||
better-call@0.2.12:
|
better-call@0.2.13:
|
||||||
resolution: {integrity: sha512-xuR4hJEtirZYI4UXOEVKaipghWcxxo4/tW3FUTIXIIG1X9nb2V/VH4HZP7fuD7ONHoNntwtPIurDNeK/SswxgA==}
|
resolution: {integrity: sha512-Y1rOwUtTYeH4usW7XNqonoo+Dtcb2sXL6m6zcLsgTYSD2ANqyELxkeA8BkCTWonN1y+Pt9CMbcn5vILaPohdUg==}
|
||||||
|
|
||||||
better-call@0.2.3-beta.2:
|
better-call@0.2.3-beta.2:
|
||||||
resolution: {integrity: sha512-ybOtGcR4pOsHI2XE+urR9zcmK+s0YnhJSx8KDj6ul7MUEyYOiMEnq/bylyH62/7qXuYb9q8Oqkp9NF9vWOZ4Mg==}
|
resolution: {integrity: sha512-ybOtGcR4pOsHI2XE+urR9zcmK+s0YnhJSx8KDj6ul7MUEyYOiMEnq/bylyH62/7qXuYb9q8Oqkp9NF9vWOZ4Mg==}
|
||||||
@@ -10369,10 +10369,12 @@ packages:
|
|||||||
|
|
||||||
libsql@0.3.19:
|
libsql@0.3.19:
|
||||||
resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==}
|
resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==}
|
||||||
|
cpu: [x64, arm64, wasm32]
|
||||||
os: [darwin, linux, win32]
|
os: [darwin, linux, win32]
|
||||||
|
|
||||||
libsql@0.4.5:
|
libsql@0.4.5:
|
||||||
resolution: {integrity: sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==}
|
resolution: {integrity: sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==}
|
||||||
|
cpu: [x64, arm64, wasm32]
|
||||||
os: [darwin, linux, win32]
|
os: [darwin, linux, win32]
|
||||||
|
|
||||||
lilconfig@2.1.0:
|
lilconfig@2.1.0:
|
||||||
@@ -23491,7 +23493,7 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
safe-buffer: 5.1.2
|
safe-buffer: 5.1.2
|
||||||
|
|
||||||
better-call@0.2.12:
|
better-call@0.2.13:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@better-fetch/fetch': 1.1.12
|
'@better-fetch/fetch': 1.1.12
|
||||||
rou3: 0.5.1
|
rou3: 0.5.1
|
||||||
|
|||||||
Reference in New Issue
Block a user