mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
feat: client side auth
This commit is contained in:
parent
a7b12bbf64
commit
dd05154be2
@ -6,6 +6,7 @@ import { useRouter, useSearchParams } from "next/navigation";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { Loader } from "./loading/Loader";
|
import { Loader } from "./loading/Loader";
|
||||||
|
import { auth } from "@/auth";
|
||||||
|
|
||||||
export function SignIn() {
|
export function SignIn() {
|
||||||
const t = useTranslations("SignIn");
|
const t = useTranslations("SignIn");
|
||||||
@ -37,20 +38,35 @@ 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('&');
|
||||||
|
|
||||||
|
await fetch("/api/auth/callback/credentials", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
body: urlEncodedData,
|
||||||
});
|
});
|
||||||
if (res?.error) {
|
|
||||||
setErrorState(true);
|
const session = await auth();
|
||||||
} else {
|
|
||||||
setErrorState(false);
|
|
||||||
|
if (session) {
|
||||||
setSuccessState(true);
|
setSuccessState(true);
|
||||||
|
} else {
|
||||||
|
setErrorState(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(false)
|
||||||
router.push("/");
|
router.push("/");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}
|
|
||||||
setLoading(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user