ResetPassword.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { useIntl } from "react-intl";
  2. import {
  3. ProForm,
  4. type ProFormInstance,
  5. ProFormText,
  6. } from "@ant-design/pro-components";
  7. import { Alert, type AlertProps, message } from "antd";
  8. import { EyeInvisibleOutlined, EyeTwoTone } from "@ant-design/icons";
  9. import { get, post } from "../../../request";
  10. import { useRef, useState } from "react";
  11. import { _Link } from "react-router";
  12. import type { RuleObject } from "antd/lib/form";
  13. import type { StoreValue } from "antd/lib/form/interface";
  14. import LoginButton from "../../auth/LoginButton";
  15. interface IFormData {
  16. username: string;
  17. token?: string | null;
  18. password?: string;
  19. confirmPassword?: string;
  20. }
  21. interface IResetPasswordResponse {
  22. ok: boolean;
  23. message: string;
  24. data: { username: string };
  25. }
  26. interface IWidget {
  27. token?: string | null;
  28. }
  29. const Widget = ({ token }: IWidget) => {
  30. const intl = useIntl();
  31. const [notify, setNotify] = useState<React.ReactNode>();
  32. const [type, setType] = useState<AlertProps["type"]>("info");
  33. const formRef = useRef<ProFormInstance | undefined>(undefined);
  34. const [ok, setOk] = useState(false);
  35. const checkPass2 = (
  36. _rule: RuleObject,
  37. value: StoreValue,
  38. callback: (error?: string) => void
  39. ) => {
  40. if (value && value !== formRef.current?.getFieldValue("password")) {
  41. callback(
  42. intl.formatMessage({
  43. id: "message.confirm-password.validate.fail",
  44. })
  45. );
  46. } else {
  47. callback();
  48. }
  49. };
  50. return (
  51. <>
  52. {notify ? (
  53. <Alert
  54. message={notify}
  55. type={type}
  56. showIcon
  57. action={ok ? <LoginButton /> : undefined}
  58. />
  59. ) : (
  60. <></>
  61. )}
  62. <ProForm<IFormData>
  63. formRef={formRef}
  64. onFinish={async (values: IFormData) => {
  65. if (!token) {
  66. return;
  67. }
  68. console.debug(values);
  69. values["token"] = token;
  70. const url = "/v2/auth/reset-password";
  71. console.info("reset password url", url, values);
  72. const result = await post<IFormData, IResetPasswordResponse>(
  73. url,
  74. values
  75. );
  76. if (result.ok) {
  77. console.log("token", result.data);
  78. setType("success");
  79. setNotify(
  80. intl.formatMessage({
  81. id: "message.password.reset.successful",
  82. })
  83. );
  84. setOk(true);
  85. message.success(intl.formatMessage({ id: "flashes.success" }));
  86. } else {
  87. setType("error");
  88. setNotify(result.message);
  89. }
  90. }}
  91. request={async () => {
  92. const url = `/v2/auth/reset-password/${token}`;
  93. console.log("url", url);
  94. try {
  95. const res = await get<IResetPasswordResponse>(url);
  96. console.log("ResetPassword get", res);
  97. if (res.ok) {
  98. setType("info");
  99. setNotify(intl.formatMessage({ id: "message.password.reset" }));
  100. return {
  101. username: res.data.username,
  102. };
  103. } else {
  104. return {
  105. username: "",
  106. };
  107. }
  108. } catch (err) {
  109. //error
  110. if (err === 404) {
  111. setNotify(intl.formatMessage({ id: "获取token失败" }));
  112. setType("error");
  113. }
  114. console.error(err);
  115. }
  116. return {
  117. username: "",
  118. };
  119. }}
  120. >
  121. <ProForm.Group>
  122. <ProFormText
  123. width="md"
  124. name="username"
  125. readonly
  126. required
  127. label={intl.formatMessage({
  128. id: "forms.fields.username.label",
  129. })}
  130. rules={[{ required: true, max: 255, min: 2 }]}
  131. />
  132. </ProForm.Group>
  133. <ProForm.Group>
  134. <ProFormText.Password
  135. width="md"
  136. name="password"
  137. fieldProps={{
  138. type: "password",
  139. iconRender: (visible) =>
  140. visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />,
  141. }}
  142. required
  143. label={intl.formatMessage({
  144. id: "forms.fields.password.label",
  145. })}
  146. rules={[{ required: true, max: 32, min: 6 }]}
  147. />
  148. </ProForm.Group>
  149. <ProForm.Group>
  150. <ProFormText.Password
  151. width="md"
  152. name="password2"
  153. fieldProps={{
  154. type: "password",
  155. iconRender: (visible) =>
  156. visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />,
  157. }}
  158. required
  159. label={intl.formatMessage({
  160. id: "forms.fields.confirm-password.label",
  161. })}
  162. rules={[{ required: true, max: 32, min: 6, validator: checkPass2 }]}
  163. />
  164. </ProForm.Group>
  165. </ProForm>
  166. </>
  167. );
  168. };
  169. export default Widget;