mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 12:27:43 +00:00
demo: fix deployment
This commit is contained in:
@@ -1,15 +1,18 @@
|
|||||||
import { client } from "@/lib/auth-client";
|
import { betterFetch } from "@better-fetch/fetch";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import type { Session } from "./lib/auth-types";
|
||||||
|
|
||||||
export default async function authMiddleware(request: NextRequest) {
|
export default async function authMiddleware(request: NextRequest) {
|
||||||
const { data: session } = await client.getSession({
|
const { data: session } = await betterFetch<Session>(
|
||||||
fetchOptions: {
|
"/api/auth/get-session",
|
||||||
|
{
|
||||||
|
baseURL: request.nextUrl.origin,
|
||||||
headers: {
|
headers: {
|
||||||
//get the cookie from the request
|
//get the cookie from the request
|
||||||
cookie: request.headers.get("cookie") || "",
|
cookie: request.headers.get("cookie") || "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return NextResponse.redirect(new URL("/", request.url));
|
return NextResponse.redirect(new URL("/", request.url));
|
||||||
|
|||||||
@@ -66,9 +66,9 @@
|
|||||||
"next": "15.0.0-canary.185",
|
"next": "15.0.0-canary.185",
|
||||||
"next-themes": "^0.3.0",
|
"next-themes": "^0.3.0",
|
||||||
"prisma": "^5.19.1",
|
"prisma": "^5.19.1",
|
||||||
"react": "19.0.0-rc-7771d3a7-20240827",
|
"react": "19.0.0-rc-cae764ce-20241025",
|
||||||
"react-day-picker": "8.10.1",
|
"react-day-picker": "8.10.1",
|
||||||
"react-dom": "19.0.0-rc-7771d3a7-20240827",
|
"react-dom": "19.0.0-rc-cae764ce-20241025",
|
||||||
"react-hook-form": "^7.53.0",
|
"react-hook-form": "^7.53.0",
|
||||||
"react-qr-code": "^2.0.15",
|
"react-qr-code": "^2.0.15",
|
||||||
"react-resizable-panels": "^2.1.2",
|
"react-resizable-panels": "^2.1.2",
|
||||||
|
|||||||
@@ -80,31 +80,38 @@ export async function ServerComponent() {
|
|||||||
|
|
||||||
## Middleware
|
## Middleware
|
||||||
|
|
||||||
In Next.js, middleware doesn’t have access to many Node APIs, so you can’t use the usual `auth` instance to validate sessions directly. Instead, you can use `authClient` and pass in the request’s cookies to check if a session is valid.
|
In Next.js, middleware doesn’t have access to many Node APIs, so you can’t use the usual `auth` instance to validate sessions directly. Instead, you can make a request to the API route to get the session using the request headers.
|
||||||
|
|
||||||
Here’s how it looks:
|
Here’s how it looks:
|
||||||
|
|
||||||
|
<Callout>
|
||||||
|
We're using `better-fetch` to make the request to the API route. You can use any fetch library you want.
|
||||||
|
</Callout>
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { authClient } from "@/lib/auth-client";
|
import { betterFetch } from "@better-fetch/fetch";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import type { Session } from "./lib/auth-types";
|
||||||
|
|
||||||
export default async function authMiddleware(request: NextRequest) {
|
export default async function authMiddleware(request: NextRequest) {
|
||||||
const { data: session } = await authClient.getSession({
|
const { data: session } = await betterFetch<Session>(
|
||||||
fetchOptions: {
|
"/api/auth/get-session",
|
||||||
headers: {
|
{
|
||||||
//get the cookie from the request
|
baseURL: request.nextUrl.origin,
|
||||||
cookie: request.headers.get("cookie") || "",
|
headers: {
|
||||||
},
|
//get the cookie from the request
|
||||||
},
|
cookie: request.headers.get("cookie") || "",
|
||||||
});
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return NextResponse.redirect(new URL("/", request.url));
|
return NextResponse.redirect(new URL("/", request.url));
|
||||||
}
|
}
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ["/dashboard/:path*"],
|
matcher: ["/dashboard"],
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
1320
pnpm-lock.yaml
generated
1320
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user