demo: fix deployment

This commit is contained in:
Bereket Engida
2024-10-26 22:13:29 +03:00
parent d706c6f1e6
commit 1d577e7faf
4 changed files with 1285 additions and 87 deletions

View File

@@ -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));

View File

@@ -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",

View File

@@ -80,31 +80,38 @@ export async function ServerComponent() {
## Middleware ## Middleware
In Next.js, middleware doesnt have access to many Node APIs, so you cant use the usual `auth` instance to validate sessions directly. Instead, you can use `authClient` and pass in the requests cookies to check if a session is valid. In Next.js, middleware doesnt have access to many Node APIs, so you cant 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.
Heres how it looks: Heres 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

File diff suppressed because it is too large Load Diff