fix: cloudflare error

This commit is contained in:
hamster1963 2024-10-21 02:19:30 +08:00
parent 2f9d1d6d5f
commit e0d6550f61
10 changed files with 67 additions and 55 deletions

View File

@ -21,7 +21,6 @@ function Header() {
const router = useRouter();
const locale = useLocale();
return (
<div className="mx-auto w-full max-w-5xl">
<section className="flex items-center justify-between">
@ -64,7 +63,7 @@ function Header() {
// https://github.com/streamich/react-use/blob/master/src/useInterval.ts
const useInterval = (callback: Function, delay?: number | null) => {
const savedCallback = useRef<Function>(() => { });
const savedCallback = useRef<Function>(() => {});
useEffect(() => {
savedCallback.current = callback;
});

View File

@ -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 <SignIn />
const session = await auth();
if (!session && getEnv("SITE_PASSWORD")) {
if (getEnv("CF_PAGES")) {
redirect("/api/auth/signin");
} else {
return <SignIn />;
}
}
return (
<div className="flex min-h-screen w-full flex-col">

View File

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

View File

@ -1,2 +1,4 @@
import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers
import { handlers } from "@/auth";
// Referring to the auth.ts we just created
export const { GET, POST } = handlers;

View File

@ -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 runtime = 'edge';
@ -14,7 +14,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 });
}

View File

@ -14,7 +14,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 });
}

View File

@ -14,7 +14,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 });
}

15
auth.ts
View File

@ -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;
},
}),
],
})
});

View File

@ -1,19 +1,18 @@
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'
"use server";
try {
await signIn("credentials", formData)
await signIn("credentials", formData);
} catch (error) {
redirect(`/${locale}`)
redirect(`/${locale}`);
}
}
@ -28,13 +27,19 @@ export function SignIn() {
<section className="flex flex-col items-start gap-2">
<label className="flex flex-col items-start gap-1 ">
<p className="text-base font-semibold"></p>
<input className="px-1 border-[1px] rounded-[5px]" name="password" type="password" />
<input
className="px-1 border-[1px] rounded-[5px]"
name="password"
type="password"
/>
</label>
<button className=" px-1.5 py-0.5 w-fit text-sm font-semibold rounded-[8px] border bg-card hover:brightness-95 transition-all text-card-foreground shadow-lg shadow-neutral-200/40 dark:shadow-none"></button>
<button className=" px-1.5 py-0.5 w-fit text-sm font-semibold rounded-[8px] border bg-card hover:brightness-95 transition-all text-card-foreground shadow-lg shadow-neutral-200/40 dark:shadow-none">
</button>
</section>
</form>
<Footer />
</main>
</div>
)
);
}

View File

@ -25,7 +25,7 @@ const nextConfig = {
reactStrictMode: true,
experimental: {
serverActions: {
allowedOrigins: ['*'],
allowedOrigins: ["*"],
},
},
logging: {