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 type { Session } from "./lib/auth-types";
|
||||
|
||||
export default async function authMiddleware(request: NextRequest) {
|
||||
const { data: session } = await client.getSession({
|
||||
fetchOptions: {
|
||||
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));
|
||||
|
||||
@@ -66,9 +66,9 @@
|
||||
"next": "15.0.0-canary.185",
|
||||
"next-themes": "^0.3.0",
|
||||
"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-dom": "19.0.0-rc-7771d3a7-20240827",
|
||||
"react-dom": "19.0.0-rc-cae764ce-20241025",
|
||||
"react-hook-form": "^7.53.0",
|
||||
"react-qr-code": "^2.0.15",
|
||||
"react-resizable-panels": "^2.1.2",
|
||||
|
||||
@@ -80,23 +80,30 @@ export async function ServerComponent() {
|
||||
|
||||
## 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:
|
||||
|
||||
<Callout>
|
||||
We're using `better-fetch` to make the request to the API route. You can use any fetch library you want.
|
||||
</Callout>
|
||||
|
||||
```ts
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import type { Session } from "./lib/auth-types";
|
||||
|
||||
export default async function authMiddleware(request: NextRequest) {
|
||||
const { data: session } = await authClient.getSession({
|
||||
fetchOptions: {
|
||||
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));
|
||||
@@ -105,6 +112,6 @@ export default async function authMiddleware(request: NextRequest) {
|
||||
}
|
||||
|
||||
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