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] 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 {