Merge pull request #80 from sijinhui/dev

opt login error
This commit is contained in:
sijinhui 2024-04-30 17:37:48 +08:00 committed by GitHub
commit 586f97c0b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,13 @@
import { signIn } from "next-auth/react"; import { signIn } from "next-auth/react";
import React, { useState, useEffect, useRef, use } from "react"; import React, { useState, useEffect, useRef, use } from "react";
import { isName } from "@/lib/auth_list"; import { isName } from "@/lib/auth_list";
import { Form, Input, InputRef } from "antd"; import {
Form,
Input,
InputRef,
notification as notificationModule,
NotificationArgsProps,
} from "antd";
import { UserOutlined, MailOutlined } from "@ant-design/icons"; import { UserOutlined, MailOutlined } from "@ant-design/icons";
import type { FormProps } from "antd"; import type { FormProps } from "antd";
import { SignInOptions } from "next-auth/react"; import { SignInOptions } from "next-auth/react";
@ -12,12 +18,28 @@ export default function UserLoginButton() {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [loginForm] = Form.useForm(); const [loginForm] = Form.useForm();
const nameInput = useRef<InputRef>(null); const nameInput = useRef<InputRef>(null);
const passwordInput = useRef<InputRef>(null); // const passwordInput = useRef<InputRef>(null);
const emailInput = useRef<HTMLInputElement>(null); // const emailInput = useRef<HTMLInputElement>(null);
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); // const [password, setPassword] = useState("");
const [notification, notificationContextHolder] =
notificationModule.useNotification();
const [error, setError] = useState(false); const openNotification = (level: string, arms: NotificationArgsProps) => {
if (level === "error") {
notification.error({
...arms,
placement: "topRight",
});
} else {
notification.info({
...arms,
placement: "topRight",
});
}
};
// const [error, setError] = useState(false);
type FieldType = { type FieldType = {
username?: string; username?: string;
password?: string; password?: string;
@ -47,6 +69,17 @@ export default function UserLoginButton() {
window.location.href = window.location.href =
result?.url && result.url.includes("verify") ? result.url : "/"; result?.url && result.url.includes("verify") ? result.url : "/";
} else { } else {
switch (result.error) {
case "AccessDenied":
openNotification("error", {
message: "登录失败",
description: "无权限,请确认用户名正确并等待审批",
});
break;
default:
break;
}
if (loginProvider === "credentials") { if (loginProvider === "credentials") {
loginForm.setFields([ loginForm.setFields([
{ {
@ -81,71 +114,68 @@ export default function UserLoginButton() {
console.log("Failed:", errorInfo); console.log("Failed:", errorInfo);
}; };
const handleNameComposition = ( // const handleNameComposition = (
e: React.CompositionEvent<HTMLInputElement>, // e: React.CompositionEvent<HTMLInputElement>,
) => { // ) => {
if (e.type === "compositionend") { // if (e.type === "compositionend") {
setUsername(e.currentTarget.value); // setUsername(e.currentTarget.value);
} // }
}; // };
const onNameChange = (e: React.ChangeEvent<HTMLInputElement>) => { // const onNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if ((e.nativeEvent as InputEvent).isComposing) { // if ((e.nativeEvent as InputEvent).isComposing) {
return; // return;
} // }
setUsername(e.target.value); // setUsername(e.target.value);
}; // };
const onPasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => { // const onPasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if ((e.nativeEvent as InputEvent).isComposing) { // if ((e.nativeEvent as InputEvent).isComposing) {
return; // return;
} // }
setPassword(e.target.value); // setPassword(e.target.value);
}; // };
const onSubmitHandler = async (e: React.FormEvent<HTMLFormElement>) => { // const onSubmitHandler = async (e: React.FormEvent<HTMLFormElement>) => {
// handle yow submition // // handle yow submition
setLoading(true); // setLoading(true);
e.preventDefault(); // e.preventDefault();
//
// let result: { error: any; url: string | null } | undefined = {
// error: null,
// url: null,
// };
// if (emailInput.current && emailInput.current.value) {
// result = await signIn("email", {
// email: emailInput.current.value,
// redirect: false,
// });
// } else {
// result = await signIn("credentials", {
// username: username,
// password: password,
// redirect: false,
// });
// }
// console.log("0000000000000", result);
// setLoading(false);
// if (!result?.error) {
// window.location.href =
// result?.url && result.url.includes("verify") ? result.url : "/";
// } else setError(true);
// };
let result: { error: any; url: string | null } | undefined = { // useEffect(() => {
error: null, // if (!username) return;
url: null, // if (nameInput.current) {
}; // if (!isName(username)) {
if (emailInput.current && emailInput.current.value) { // setError(true);
result = await signIn("email", { // } else {
email: emailInput.current.value, // setError(false);
redirect: false, // }
}); // }
} else { // }, [username]);
result = await signIn("credentials", {
username: username,
password: password,
redirect: false,
});
}
console.log("0000000000000", result);
setLoading(false);
if (!result?.error) {
window.location.href =
result?.url && result.url.includes("verify") ? result.url : "/";
} else setError(true);
};
useEffect(() => {
if (!username) return;
if (nameInput.current) {
if (!isName(username)) {
setError(true);
// nameInput
// nameInput.current.setCustomValidity("用户名校验失败");
} else {
setError(false);
// nameInput.current.setCustomValidity("");
}
}
// console.log("username:", username);
}, [username]);
return ( return (
<> <>
{notificationContextHolder}
<div className="mt-6 sm:mx-auto sm:w-full sm:max-w-sm"> <div className="mt-6 sm:mx-auto sm:w-full sm:max-w-sm">
<Form <Form
className="space-y-6" className="space-y-6"