SignIn.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { useIntl } from "react-intl";
  2. import { ProForm, ProFormText } from "@ant-design/pro-components";
  3. import { Alert, message } from "antd";
  4. import { useNavigate, useParams, useSearchParams } from "react-router-dom";
  5. import { EyeInvisibleOutlined, EyeTwoTone } from "@ant-design/icons";
  6. import { useAppDispatch } from "../../../hooks";
  7. import { IUser, signIn, TO_HOME } from "../../../reducers/current-user";
  8. import { get, post } from "../../../request";
  9. import { useState } from "react";
  10. interface IFormData {
  11. email: string;
  12. password: string;
  13. }
  14. interface ISignInResponse {
  15. ok: boolean;
  16. message: string;
  17. data: string;
  18. }
  19. interface IUserResponse {
  20. ok: boolean;
  21. message: string;
  22. data: IUser;
  23. }
  24. interface ISignInRequest {
  25. username: string;
  26. password: string;
  27. }
  28. const Widget = () => {
  29. const intl = useIntl();
  30. const dispatch = useAppDispatch();
  31. const navigate = useNavigate();
  32. const [error, setError] = useState<string>();
  33. const [searchParams] = useSearchParams();
  34. return (
  35. <>
  36. {error ? <Alert message={error} type="error" /> : undefined}
  37. <ProForm<IFormData>
  38. onFinish={async (values: IFormData) => {
  39. setError(undefined);
  40. const user = {
  41. username: values.email.trim(),
  42. password: values.password.trim(),
  43. };
  44. const res = await post<ISignInRequest, ISignInResponse>(
  45. "/v2/sign-in",
  46. user
  47. );
  48. if (res.ok) {
  49. localStorage.setItem("token", res.data);
  50. get<IUserResponse>("/v2/auth/current").then((json) => {
  51. if (json.ok) {
  52. dispatch(signIn([json.data, res.data]));
  53. let url: string | null = null;
  54. searchParams.forEach((value, key) => {
  55. if (key === "url") {
  56. url = value;
  57. }
  58. });
  59. if (url) {
  60. window.location.href = atob(url);
  61. } else {
  62. navigate(TO_HOME);
  63. }
  64. } else {
  65. setError("用户名或密码错误");
  66. console.error(json.message);
  67. }
  68. });
  69. message.success(intl.formatMessage({ id: "flashes.success" }));
  70. } else {
  71. setError("用户名或密码错误");
  72. }
  73. }}
  74. >
  75. <ProForm.Group>
  76. <ProFormText
  77. width="md"
  78. name="email"
  79. required
  80. label={intl.formatMessage({
  81. id: "forms.fields.email.or.username.label",
  82. })}
  83. rules={[{ required: true, max: 255, min: 4 }]}
  84. />
  85. </ProForm.Group>
  86. <ProForm.Group>
  87. <ProFormText.Password
  88. width="md"
  89. name="password"
  90. fieldProps={{
  91. iconRender: (visible) =>
  92. visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />,
  93. }}
  94. required
  95. label={intl.formatMessage({
  96. id: "forms.fields.password.label",
  97. })}
  98. rules={[{ required: true, max: 32, min: 4 }]}
  99. />
  100. </ProForm.Group>
  101. </ProForm>
  102. </>
  103. );
  104. };
  105. export default Widget;