mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
commit
b6980c9daa
@ -1,8 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { getCsrfToken, signIn } from "next-auth/react";
|
import { getCsrfToken } from "next-auth/react";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import { useRouter, useSearchParams } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { Loader } from "./loading/Loader";
|
import { Loader } from "./loading/Loader";
|
||||||
@ -11,20 +11,11 @@ export function SignIn() {
|
|||||||
const t = useTranslations("SignIn");
|
const t = useTranslations("SignIn");
|
||||||
|
|
||||||
const [csrfToken, setCsrfToken] = useState("");
|
const [csrfToken, setCsrfToken] = useState("");
|
||||||
const [errorState, setErrorState] = useState(false);
|
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [successState, setSuccessState] = useState(false);
|
const [errorState, setErrorState] = useState(false);
|
||||||
|
|
||||||
const search = useSearchParams();
|
|
||||||
const error = search.get("error");
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (error) {
|
|
||||||
setErrorState(true);
|
|
||||||
}
|
|
||||||
}, [error]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function loadProviders() {
|
async function loadProviders() {
|
||||||
const csrf = await getCsrfToken();
|
const csrf = await getCsrfToken();
|
||||||
@ -37,20 +28,32 @@ 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");
|
|
||||||
const res = await signIn("credentials", {
|
|
||||||
password,
|
// 直接构建 URL 编码的字符串
|
||||||
redirect: false,
|
const urlEncodedData = [
|
||||||
|
`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) {
|
|
||||||
setErrorState(true);
|
if (res.url.includes("error")) {
|
||||||
|
setLoading(false)
|
||||||
|
setErrorState(true)
|
||||||
} else {
|
} else {
|
||||||
setErrorState(false);
|
setLoading(false)
|
||||||
setSuccessState(true);
|
setErrorState(false)
|
||||||
router.push("/");
|
|
||||||
router.refresh();
|
|
||||||
}
|
}
|
||||||
setLoading(false);
|
router.push("/");
|
||||||
|
router.refresh();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -66,11 +69,6 @@ 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]"
|
||||||
|
Loading…
Reference in New Issue
Block a user