feat: add success state

This commit is contained in:
hamster1963 2024-10-21 12:32:29 +08:00
parent 17a9b20cda
commit 8e5ddd6de8
6 changed files with 28 additions and 7 deletions

View File

@ -5,7 +5,7 @@ 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: { pages: {
signIn: "/", signIn: "/",
}, },

View File

@ -2,14 +2,18 @@
import { getCsrfToken, signIn } from "next-auth/react"; import { getCsrfToken, signIn } from "next-auth/react";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import { useSearchParams, useRouter } from "next/navigation"; import { useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Loader } from "./loading/Loader";
export function SignIn() { 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 [errorState, setErrorState] = useState(false);
const [loading, setLoading] = useState(false);
const [successState, setSuccessState] = useState(false);
const search = useSearchParams(); const search = useSearchParams();
const error = search.get("error"); const error = search.get("error");
@ -31,6 +35,7 @@ export function SignIn() {
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
setLoading(true);
const formData = new FormData(e.currentTarget); const formData = new FormData(e.currentTarget);
const password = formData.get("password"); const password = formData.get("password");
const res = await signIn("credentials", { const res = await signIn("credentials", {
@ -40,9 +45,12 @@ export function SignIn() {
if (res?.error) { if (res?.error) {
setErrorState(true); setErrorState(true);
} else { } else {
setErrorState(false);
setSuccessState(true);
router.push("/"); router.push("/");
router.refresh(); router.refresh();
} }
setLoading(false);
}; };
return ( return (
@ -58,6 +66,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]"
@ -65,8 +78,12 @@ export function SignIn() {
type="password" type="password"
/> />
</label> </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
className=" px-1.5 py-0.5 w-fit flex items-center gap-1 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"
disabled={loading}
>
{t("Submit")} {t("Submit")}
{loading && <Loader visible={true} />}
</button> </button>
</section> </section>
</form> </form>

View File

@ -26,7 +26,8 @@
"SignIn": { "SignIn": {
"SignInMessage": "Please enter the password", "SignInMessage": "Please enter the password",
"Submit": "Login", "Submit": "Login",
"ErrorMessage": "Invalid password" "ErrorMessage": "Invalid password",
"SuccessMessage": "Login successful"
}, },
"ServerCardPopover": { "ServerCardPopover": {
"System": "System", "System": "System",

View File

@ -26,7 +26,8 @@
"SignIn": { "SignIn": {
"SignInMessage": "パスワードを入力してください", "SignInMessage": "パスワードを入力してください",
"Submit": "ログイン", "Submit": "ログイン",
"ErrorMessage": "パスワードが間違っています" "ErrorMessage": "パスワードが間違っています",
"SuccessMessage": "ログイン成功"
}, },
"ServerCardPopover": { "ServerCardPopover": {
"System": "システム", "System": "システム",

View File

@ -26,7 +26,8 @@
"SignIn": { "SignIn": {
"SignInMessage": "請輸入密碼", "SignInMessage": "請輸入密碼",
"Submit": "登入", "Submit": "登入",
"ErrorMessage": "密碼錯誤" "ErrorMessage": "密碼錯誤",
"SuccessMessage": "登入成功"
}, },
"ServerCardPopover": { "ServerCardPopover": {
"System": "系統", "System": "系統",

View File

@ -26,7 +26,8 @@
"SignIn": { "SignIn": {
"SignInMessage": "请输入密码", "SignInMessage": "请输入密码",
"Submit": "登录", "Submit": "登录",
"ErrorMessage": "密码错误" "ErrorMessage": "密码错误",
"SuccessMessage": "登录成功"
}, },
"ServerCardPopover": { "ServerCardPopover": {
"System": "系统", "System": "系统",