diff --git a/app/[locale]/(main)/header.tsx b/app/[locale]/(main)/header.tsx index a7d9ec7..523b2d1 100644 --- a/app/[locale]/(main)/header.tsx +++ b/app/[locale]/(main)/header.tsx @@ -20,7 +20,6 @@ function Header() { const router = useRouter(); const locale = useLocale(); - return (
@@ -63,7 +62,7 @@ function Header() { // https://github.com/streamich/react-use/blob/master/src/useInterval.ts const useInterval = (callback: Function, delay?: number | null) => { - const savedCallback = useRef(() => { }); + const savedCallback = useRef(() => {}); useEffect(() => { savedCallback.current = callback; }); diff --git a/app/[locale]/(main)/layout.tsx b/app/[locale]/(main)/layout.tsx index 82a15af..ef107f1 100644 --- a/app/[locale]/(main)/layout.tsx +++ b/app/[locale]/(main)/layout.tsx @@ -3,14 +3,22 @@ import Header from "@/app/[locale]/(main)/header"; import { auth } from "@/auth"; import { SignIn } from "@/components/sign-in"; import getEnv from "@/lib/env-entry"; +import { redirect } from "next/navigation"; import React from "react"; type DashboardProps = { children: React.ReactNode; }; export default async function MainLayout({ children }: DashboardProps) { - const session = await auth() - if (!session && getEnv("SITE_PASSWORD")) return + const session = await auth(); + + if (!session && getEnv("SITE_PASSWORD")) { + if (getEnv("CF_PAGES")) { + redirect("/api/auth/signin"); + } else { + return ; + } + } return (
diff --git a/app/[locale]/layout.tsx b/app/[locale]/layout.tsx index 2b2864f..9e6ce07 100644 --- a/app/[locale]/layout.tsx +++ b/app/[locale]/layout.tsx @@ -1,4 +1,5 @@ // @auto-i18n-check. Please do not delete the line. +import { auth } from "@/auth"; import { locales } from "@/i18n-metadata"; import getEnv from "@/lib/env-entry"; import { cn } from "@/lib/utils"; @@ -13,7 +14,6 @@ import { Inter as FontSans } from "next/font/google"; import React from "react"; import "/node_modules/flag-icons/css/flag-icons.min.css"; -import { auth } from "@/auth"; const fontSans = FontSans({ subsets: ["latin"], diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 0cf1408..87f6488 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,2 +1,4 @@ -import { handlers } from "@/auth" // Referring to the auth.ts we just created -export const { GET, POST } = handlers \ No newline at end of file +import { handlers } from "@/auth"; + +// Referring to the auth.ts we just created +export const { GET, POST } = handlers; diff --git a/app/api/detail/route.ts b/app/api/detail/route.ts index fdc18cb..17ef913 100644 --- a/app/api/detail/route.ts +++ b/app/api/detail/route.ts @@ -1,8 +1,8 @@ import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; +import { auth } from "@/auth"; +import getEnv from "@/lib/env-entry"; import { GetServerDetail } from "@/lib/serverFetch"; import { NextResponse } from "next/server"; -import { auth } from "@/auth" -import getEnv from "@/lib/env-entry"; export const dynamic = "force-dynamic"; @@ -12,7 +12,6 @@ interface NezhaDataResponse { } export const GET = auth(async function GET(req) { - if (!req.auth && getEnv("SITE_PASSWORD")) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); } diff --git a/app/api/monitor/route.ts b/app/api/monitor/route.ts index 3f38586..528de95 100644 --- a/app/api/monitor/route.ts +++ b/app/api/monitor/route.ts @@ -12,7 +12,6 @@ interface NezhaDataResponse { } export const GET = auth(async function GET(req) { - if (!req.auth && getEnv("SITE_PASSWORD")) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); } diff --git a/app/api/server/route.ts b/app/api/server/route.ts index e6a1f99..f201247 100644 --- a/app/api/server/route.ts +++ b/app/api/server/route.ts @@ -12,7 +12,6 @@ interface NezhaDataResponse { } export const GET = auth(async function GET(req) { - if (!req.auth && getEnv("SITE_PASSWORD")) { return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); } diff --git a/auth.ts b/auth.ts index a908388..ca98657 100644 --- a/auth.ts +++ b/auth.ts @@ -1,9 +1,10 @@ -import NextAuth from "next-auth" -import Credentials from "next-auth/providers/credentials" -import getEnv from "./lib/env-entry" - +import NextAuth from "next-auth"; +import Credentials from "next-auth/providers/credentials"; + +import getEnv from "./lib/env-entry"; + export const { handlers, signIn, signOut, auth } = NextAuth({ - secret:"this_is_nezha_dash_web_secret", + secret: "this_is_nezha_dash_web_secret", providers: [ Credentials({ credentials: { @@ -11,10 +12,10 @@ export const { handlers, signIn, signOut, auth } = NextAuth({ }, authorize: async (credentials) => { if (credentials.password === getEnv("SITE_PASSWORD")) { - return { id: "0" } + return { id: "0" }; } - return null + return null; }, }), ], -}) \ No newline at end of file +}); diff --git a/components/sign-in.tsx b/components/sign-in.tsx index 8d266c5..c8adbea 100644 --- a/components/sign-in.tsx +++ b/components/sign-in.tsx @@ -1,40 +1,45 @@ -import Footer from "@/app/[locale]/(main)/footer" -import Header from "@/app/[locale]/(main)/header" -import { signIn } from "@/auth" -import { useLocale } from "next-intl" -import { redirect } from "next/navigation" +import Footer from "@/app/[locale]/(main)/footer"; +import Header from "@/app/[locale]/(main)/header"; +import { signIn } from "@/auth"; +import { useLocale } from "next-intl"; +import { redirect } from "next/navigation"; export function SignIn() { - const locale = useLocale() + const locale = useLocale(); - - async function handleSubmit(formData: FormData) { - 'use server' - try { - await signIn("credentials", formData) - } catch (error) { - redirect(`/${locale}`) - } + async function handleSubmit(formData: FormData) { + "use server"; + try { + await signIn("credentials", formData); + } catch (error) { + redirect(`/${locale}`); } + } - return ( -
-
-
-
-
- - -
-
-
-
-
- ) + return ( +
+
+
+
+
+ + +
+
+
+
+ ); } diff --git a/next.config.mjs b/next.config.mjs index 5713407..a68be47 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -25,7 +25,7 @@ const nextConfig = { reactStrictMode: true, experimental: { serverActions: { - allowedOrigins: ['*'], + allowedOrigins: ["*"], }, }, logging: {