From dd05154be2e70a76082f37434c61f31de387fa98 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Mon, 21 Oct 2024 13:10:59 +0800 Subject: [PATCH] feat: client side auth --- components/SignIn.tsx | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/components/SignIn.tsx b/components/SignIn.tsx index 31e6913..e49da09 100644 --- a/components/SignIn.tsx +++ b/components/SignIn.tsx @@ -6,6 +6,7 @@ import { useRouter, useSearchParams } from "next/navigation"; import { useEffect, useState } from "react"; import { Loader } from "./loading/Loader"; +import { auth } from "@/auth"; export function SignIn() { const t = useTranslations("SignIn"); @@ -37,20 +38,35 @@ export function SignIn() { e.preventDefault(); setLoading(true); const formData = new FormData(e.currentTarget); - const password = formData.get("password"); - const res = await signIn("credentials", { - password, - redirect: false, + + + // 直接构建 URL 编码的字符串 + 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); - } else { - setErrorState(false); + + const session = await auth(); + + + if (session) { setSuccessState(true); - router.push("/"); - router.refresh(); + } else { + setErrorState(true); } - setLoading(false); + + setLoading(false) + router.push("/"); + router.refresh(); }; return (