Explorar o código

login 允许 username & email

visuddhinanda %!s(int64=3) %!d(string=hai) anos
pai
achega
c3a98623a9
Modificáronse 1 ficheiros con 67 adicións e 71 borrados
  1. 67 71
      dashboard/src/components/nut/users/SignIn.tsx

+ 67 - 71
dashboard/src/components/nut/users/SignIn.tsx

@@ -10,86 +10,82 @@ import { get, post } from "../../../request";
 import store from "../../../store";
 
 interface IFormData {
-	email: string;
-	password: string;
+  email: string;
+  password: string;
 }
 interface ISignInResponse {
-	ok: boolean;
-	message: string;
-	data: string;
+  ok: boolean;
+  message: string;
+  data: string;
 }
 interface IUserResponse {
-	ok: boolean;
-	message: string;
-	data: IUser;
+  ok: boolean;
+  message: string;
+  data: IUser;
 }
 interface ISignInRequest {
-	username: string;
-	password: string;
+  username: string;
+  password: string;
 }
 const Widget = () => {
-	const intl = useIntl();
-	const dispatch = useAppDispatch();
-	const navigate = useNavigate();
+  const intl = useIntl();
+  const dispatch = useAppDispatch();
+  const navigate = useNavigate();
 
-	return (
-		<ProForm<IFormData>
-			onFinish={async (values: IFormData) => {
-				// TODO
-				console.log(values);
-				const user = {
-					username: values.email,
-					password: values.password,
-				};
-				const signin = await post<ISignInRequest, ISignInResponse>(
-					"/v2/auth/signin",
-					user
-				);
-				if (signin.ok) {
-					console.log("token", signin.data);
-					localStorage.setItem("token", signin.data);
-					get<IUserResponse>("/v2/auth/current").then((json) => {
-						if (json.ok) {
-							dispatch(signIn([json.data, signin.data]));
-							navigate(TO_HOME);
-						} else {
-							console.error(json.message);
-						}
-					});
-					message.success(
-						intl.formatMessage({ id: "flashes.success" })
-					);
-				} else {
-					message.error(signin.message);
-				}
-			}}
-		>
-			<ProForm.Group>
-				<ProFormText
-					width="md"
-					name="email"
-					required
-					label={intl.formatMessage({
-						id: "forms.fields.email.label",
-					})}
-					rules={[
-						{ required: true, type: "email", max: 255, min: 6 },
-					]}
-				/>
-			</ProForm.Group>
-			<ProForm.Group>
-				<ProFormText
-					width="md"
-					name="password"
-					required
-					label={intl.formatMessage({
-						id: "forms.fields.password.label",
-					})}
-					rules={[{ required: true, max: 32, min: 4 }]}
-				/>
-			</ProForm.Group>
-		</ProForm>
-	);
+  return (
+    <ProForm<IFormData>
+      onFinish={async (values: IFormData) => {
+        // TODO
+        console.log(values);
+        const user = {
+          username: values.email,
+          password: values.password,
+        };
+        const signin = await post<ISignInRequest, ISignInResponse>(
+          "/v2/auth/signin",
+          user
+        );
+        if (signin.ok) {
+          console.log("token", signin.data);
+          localStorage.setItem("token", signin.data);
+          get<IUserResponse>("/v2/auth/current").then((json) => {
+            if (json.ok) {
+              dispatch(signIn([json.data, signin.data]));
+              navigate(TO_HOME);
+            } else {
+              console.error(json.message);
+            }
+          });
+          message.success(intl.formatMessage({ id: "flashes.success" }));
+        } else {
+          message.error(signin.message);
+        }
+      }}
+    >
+      <ProForm.Group>
+        <ProFormText
+          width="md"
+          name="email"
+          required
+          label={intl.formatMessage({
+            id: "forms.fields.email.label",
+          })}
+          rules={[{ required: true, max: 255, min: 6 }]}
+        />
+      </ProForm.Group>
+      <ProForm.Group>
+        <ProFormText
+          width="md"
+          name="password"
+          required
+          label={intl.formatMessage({
+            id: "forms.fields.password.label",
+          })}
+          rules={[{ required: true, max: 32, min: 4 }]}
+        />
+      </ProForm.Group>
+    </ProForm>
+  );
 };
 
 export default Widget;