From b123ad9f3e277fc5675bde648e1151a4201d129c Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Wed, 6 Nov 2024 15:29:44 +0800 Subject: [PATCH 1/4] perf(layout): refactor auth check --- app/(main)/layout.tsx | 16 ++++++++++++---- app/api/detail/route.ts | 9 +++++---- app/api/monitor/route.ts | 10 ++++++---- app/api/server/route.ts | 9 +++++---- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/app/(main)/layout.tsx b/app/(main)/layout.tsx index 9df1909..8df46a4 100644 --- a/app/(main)/layout.tsx +++ b/app/(main)/layout.tsx @@ -8,16 +8,24 @@ import React from "react"; type DashboardProps = { children: React.ReactNode; }; -export default async function MainLayout({ children }: DashboardProps) { - const session = await auth(); - +export default function MainLayout({ children }: DashboardProps) { return (
- {!session && getEnv("SitePassword") ? : children} + {children}
); } + +async function AuthProtected({ children }: DashboardProps) { + if (getEnv("SitePassword")) { + const session = await auth(); + if (!session) { + return ; + } + } + return children; +} diff --git a/app/api/detail/route.ts b/app/api/detail/route.ts index 91ee0e6..ead6722 100644 --- a/app/api/detail/route.ts +++ b/app/api/detail/route.ts @@ -12,10 +12,11 @@ interface ResError extends Error { } export async function GET(req: NextRequest) { - const session = await auth(); - - if (!session && getEnv("SitePassword")) { - redirect("/"); + if (getEnv("SitePassword")) { + const session = await auth(); + if (!session) { + redirect("/"); + } } const { searchParams } = new URL(req.url); diff --git a/app/api/monitor/route.ts b/app/api/monitor/route.ts index beb8326..82a4ceb 100644 --- a/app/api/monitor/route.ts +++ b/app/api/monitor/route.ts @@ -12,14 +12,16 @@ interface ResError extends Error { } export async function GET(req: NextRequest) { - const session = await auth(); - - if (!session && getEnv("SitePassword")) { - redirect("/"); + if (getEnv("SitePassword")) { + const session = await auth(); + if (!session) { + redirect("/"); + } } const { searchParams } = new URL(req.url); const server_id = searchParams.get("server_id"); + if (!server_id) { return NextResponse.json( { error: "server_id is required" }, diff --git a/app/api/server/route.ts b/app/api/server/route.ts index f54fdec..5c97021 100644 --- a/app/api/server/route.ts +++ b/app/api/server/route.ts @@ -12,10 +12,11 @@ interface ResError extends Error { } export async function GET() { - const session = await auth(); - - if (!session && getEnv("SitePassword")) { - redirect("/"); + if (getEnv("SitePassword")) { + const session = await auth(); + if (!session) { + redirect("/"); + } } try { From adaed58fa5b13c357e11f3c00cf8b639c524881c Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Wed, 6 Nov 2024 15:38:00 +0800 Subject: [PATCH 2/4] test: client side auth --- app/(main)/layout.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/(main)/layout.tsx b/app/(main)/layout.tsx index 8df46a4..963a411 100644 --- a/app/(main)/layout.tsx +++ b/app/(main)/layout.tsx @@ -1,3 +1,5 @@ +"use client"; + import Footer from "@/app/(main)/footer"; import Header from "@/app/(main)/header"; import { auth } from "@/auth"; From 2462659026f785ecf093abbf685b81c30522c5ea Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Wed, 6 Nov 2024 15:48:47 +0800 Subject: [PATCH 3/4] fix(layout): revert client side auth --- app/(main)/layout.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/(main)/layout.tsx b/app/(main)/layout.tsx index 963a411..8df46a4 100644 --- a/app/(main)/layout.tsx +++ b/app/(main)/layout.tsx @@ -1,5 +1,3 @@ -"use client"; - import Footer from "@/app/(main)/footer"; import Header from "@/app/(main)/header"; import { auth } from "@/auth"; From 913a9051004bd33bf955f6f794c433e99336fa45 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Wed, 6 Nov 2024 15:57:17 +0800 Subject: [PATCH 4/4] perf(serverless): fallback to static env --- app/layout.tsx | 2 +- lib/env-entry.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/layout.tsx b/app/layout.tsx index a4deb23..9fb6075 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -48,7 +48,7 @@ export default async function LocaleLayout({ return ( - + {!process.env.VERCEL && !process.env.CF_PAGES && }