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/8] 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/8] 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/8] 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/8] 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 && } Date: Wed, 6 Nov 2024 16:23:14 +0800 Subject: [PATCH 5/8] fix(cloudflare): get env --- app/layout.tsx | 2 +- lib/env-entry.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/layout.tsx b/app/layout.tsx index 9fb6075..fc1eeca 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 && } + {!process.env.VERCEL && } Date: Wed, 6 Nov 2024 16:29:55 +0800 Subject: [PATCH 6/8] update: v1.2.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0208f46..520ba39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nezha-dash", - "version": "1.2.4", + "version": "1.2.5", "private": true, "scripts": { "dev": "next dev -p 3020", From b498b67ea34f7dc435c7904ca53dcc954e7636ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=93=E9=BC=A0?= <71394853+hamster1963@users.noreply.github.com> Date: Wed, 6 Nov 2024 18:39:51 +0800 Subject: [PATCH 7/8] fix: vercel env --- app/layout.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/layout.tsx b/app/layout.tsx index fc1eeca..a4deb23 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -48,7 +48,7 @@ export default async function LocaleLayout({ return ( - {!process.env.VERCEL && } + Date: Wed, 6 Nov 2024 18:40:58 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix=EF=BC=9A=20vercel=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/env-entry.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/env-entry.ts b/lib/env-entry.ts index a18b319..caf79fc 100644 --- a/lib/env-entry.ts +++ b/lib/env-entry.ts @@ -1,12 +1,8 @@ import { env } from "next-runtime-env"; export default function getEnv(key: string) { - if (process.env.VERCEL) { - return process.env[key]; - } else { if (key.startsWith("NEXT_PUBLIC_")) { return env(key); } return process.env[key]; - } }