Merge branch 'main' into cloudflare

This commit is contained in:
hamster1963 2024-10-21 14:56:59 +08:00
commit c4fe7d8b2f
3 changed files with 44 additions and 37 deletions

View File

@ -5,10 +5,10 @@
</div> </div>
| 一键部署到 Vercel-推荐 | Docker部署 | Cloudflare部署 | 如何更新? | | 一键部署到 Vercel-推荐 | Docker部署 | Cloudflare部署 | 如何更新? |
| ----------------------------------------------------- | --------------------------------------------------------------- | ----------------------------------------------------------------------- | --------------------------------------------------------- | | ----------------------------------------------------- | --------------------------------------------------------------- | ---------------------------------------------------------------------------- | --------------------------------------------------------- |
| [部署简易教程](https://buycoffee.top/blog/tech/nezha) | [Docker 部署教程](https://buycoffee.top/blog/tech/nezha-docker) | [Cloudflare 部署教程](https://buycoffee.top/blog/tech/nezha-cloudflare) | [更新教程](https://buycoffee.top/blog/tech/nezha-upgrade) | | [部署简易教程](https://buycoffee.top/blog/tech/nezha) | [Docker 部署教程](https://buycoffee.top/blog/tech/nezha-docker) | [Cloudflare 部署教程](https://buycoffee.top/blog/tech/nezha-cloudflare) | [更新教程](https://buycoffee.top/blog/tech/nezha-upgrade) |
| [Vercel-demo](https://nezha-vercel.buycoffee.top) | [Docker-demo](https://nezha-docker.buycoffee.tech) | [Cloudflare-demo](https://nezha-cloudflare.buycoffee.tech) [密码: nezhadash] | | [Vercel-demo](https://nezha-vercel.buycoffee.top) | [Docker-demo](https://nezha-docker.buycoffee.tech) | [Cloudflare-demo](https://nezha-cloudflare.buycoffee.tech) [密码: nezhadash] |
#### 环境变量 #### 环境变量

25
auth.ts
View File

@ -1,23 +1,32 @@
import NextAuth from "next-auth"; import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials"; import CredentialsProvider from "next-auth/providers/credentials";
import getEnv from "./lib/env-entry"; import getEnv from "./lib/env-entry";
export const { handlers, signIn, signOut, auth } = NextAuth({ export const { handlers, signIn, signOut, auth } = NextAuth({
secret: process.env.AUTH_SECRET ?? "this_is_nezha_dash_web_secret", secret: process.env.AUTH_SECRET ?? "this_is_nezha_dash_web_secret",
trustHost: (process.env.AUTH_TRUST_HOST as boolean | undefined) ?? true, trustHost: (process.env.AUTH_TRUST_HOST as boolean | undefined) ?? true,
pages: {
signIn: "/",
},
providers: [ providers: [
Credentials({ CredentialsProvider({
type: "credentials",
credentials: { password: { label: "Password", type: "password" } }, credentials: { password: { label: "Password", type: "password" } },
authorize: async (credentials) => { // authorization function
if (credentials.password === getEnv("SitePassword")) { async authorize(credentials) {
const { password } = credentials;
if (password === getEnv("SitePassword")) {
return { id: "nezha-dash-auth" }; return { id: "nezha-dash-auth" };
} }
return null; return { error: "Invalid password" };
}, },
}), }),
], ],
callbacks: {
async signIn({ user }) {
// @ts-ignore
if (user.error) {
return false;
}
return true;
},
},
}); });

View File

@ -1,6 +1,6 @@
"use client"; "use client";
import { getCsrfToken } from "next-auth/react"; import { getCsrfToken, signIn } from "next-auth/react";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
@ -13,6 +13,7 @@ export function SignIn() {
const [csrfToken, setCsrfToken] = useState(""); const [csrfToken, setCsrfToken] = useState("");
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [errorState, setErrorState] = useState(false); const [errorState, setErrorState] = useState(false);
const [successState, setSuccessState] = useState(false);
const router = useRouter(); const router = useRouter();
@ -28,34 +29,26 @@ export function SignIn() {
e.preventDefault(); e.preventDefault();
setLoading(true); setLoading(true);
const formData = new FormData(e.currentTarget); const formData = new FormData(e.currentTarget);
const password = formData.get("password") as string;
const res = await signIn("credentials", {
// 直接构建 URL 编码的字符串 password: password,
const urlEncodedData = [ redirect: false,
`csrfToken=${encodeURIComponent(csrfToken)}`,
`redirect=false`,
`password=${encodeURIComponent(formData.get('password') as string)}`,
].join('&');
const res = await fetch("/api/auth/callback/credentials", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: urlEncodedData,
}); });
if (res?.error) {
if (res.url.includes("error")) { console.log("login error");
setLoading(false) console.log(res);
setErrorState(true) setErrorState(true);
setSuccessState(false);
} else { } else {
setLoading(false) console.log("login success");
setErrorState(false) console.log(res);
setErrorState(false);
setSuccessState(true);
router.push("/");
router.refresh();
} }
router.push("/"); setLoading(false);
router.refresh();
}; };
return ( return (
<form <form
className="flex flex-col items-center justify-start gap-4 p-4 " className="flex flex-col items-center justify-start gap-4 p-4 "
@ -69,6 +62,11 @@ export function SignIn() {
{t("ErrorMessage")} {t("ErrorMessage")}
</p> </p>
)} )}
{successState && (
<p className="text-green-500 text-sm font-semibold">
{t("SuccessMessage")}
</p>
)}
<p className="text-base font-semibold">{t("SignInMessage")}</p> <p className="text-base font-semibold">{t("SignInMessage")}</p>
<input <input
className="px-1 border-[1px] rounded-[5px]" className="px-1 border-[1px] rounded-[5px]"