ForgotPasswordModal.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import api from '@/lib/api';
  4. import { X, Lock, Loader2, Save, Mail, ArrowRight, KeyRound, Eye, EyeOff } from 'lucide-react';
  5. import { useLanguage } from '@/lib/i18n/LanguageContext';
  6. import MessageModal from '@/components/common/MessageModal';
  7. interface ForgotPasswordModalProps {
  8. isOpen: boolean;
  9. onClose: () => void;
  10. }
  11. export default function ForgotPasswordModal({ isOpen, onClose }: ForgotPasswordModalProps) {
  12. const { t } = useLanguage();
  13. const [step, setStep] = useState<1 | 2>(1);
  14. const [loading, setLoading] = useState(false);
  15. const [countdown, setCountdown] = useState(0);
  16. const [email, setEmail] = useState('');
  17. const [code, setCode] = useState('');
  18. const [newPassword, setNewPassword] = useState('');
  19. const [showPassword, setShowPassword] = useState(false);
  20. const [msgModal, setMsgModal] = useState({
  21. isOpen: false,
  22. title: '',
  23. message: '',
  24. type: 'info' as 'info' | 'error' | 'success',
  25. onOk: null as (() => void) | null,
  26. });
  27. const showMessage = (msg: string, type: 'info' | 'error' | 'success' = 'info', onOk?: () => void) => {
  28. setMsgModal({
  29. isOpen: true,
  30. title: type === 'error' ? t('common.error') : t('common.notice'),
  31. message: msg,
  32. type,
  33. onOk: onOk || null,
  34. });
  35. };
  36. const handleCloseMsg = () => {
  37. const callback = msgModal.onOk;
  38. setMsgModal((prev) => ({ ...prev, isOpen: false }));
  39. if (callback) callback();
  40. };
  41. useEffect(() => {
  42. let timer: NodeJS.Timeout;
  43. if (countdown > 0) {
  44. timer = setTimeout(() => setCountdown(c => c - 1), 1000);
  45. }
  46. return () => clearTimeout(timer);
  47. }, [countdown]);
  48. useEffect(() => {
  49. if (isOpen) {
  50. setStep(1);
  51. setCode('');
  52. setNewPassword('');
  53. }
  54. }, [isOpen]);
  55. const handleSendCode = async (e: React.FormEvent) => {
  56. e.preventDefault();
  57. if (!email) return showMessage(t('forgot_password.enter_email_alert'), 'error');
  58. setLoading(true);
  59. try {
  60. await api.post('/api/auth/send-reset-code', { email });
  61. showMessage(`${t('forgot_password.code_sent_alert')} ${email}`, 'success');
  62. setStep(2);
  63. setCountdown(60);
  64. } catch (error: any) {
  65. console.error(error);
  66. const msg = error.response?.data?.message || t('forgot_password.send_failed_default');
  67. showMessage(`${t('forgot_password.send_failed')}: ${msg}`, 'error');
  68. } finally {
  69. setLoading(false);
  70. }
  71. };
  72. const handleSubmitReset = async (e: React.FormEvent) => {
  73. e.preventDefault();
  74. if (!code) return showMessage(t('forgot_password.enter_code_alert'), 'error');
  75. if (!newPassword) return showMessage(t('forgot_password.enter_password_alert'), 'error');
  76. setLoading(true);
  77. try {
  78. await api.post('/api/auth/reset-password', {
  79. email,
  80. code,
  81. new_password: newPassword
  82. });
  83. showMessage(t('forgot_password.reset_success'), 'success', () => {
  84. onClose();
  85. });
  86. } catch (error: any) {
  87. console.error(error);
  88. const msg = error.response?.data?.message || t('forgot_password.code_error');
  89. showMessage(`${t('forgot_password.reset_failed')}: ${msg}`, 'error');
  90. } finally {
  91. setLoading(false);
  92. }
  93. };
  94. if (!isOpen) return null;
  95. return (
  96. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4 font-mono">
  97. <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-md overflow-hidden animate-in zoom-in-95 duration-200">
  98. {/* Header */}
  99. <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
  100. <div className="flex items-center gap-2">
  101. <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
  102. <h3 className="font-black text-black text-xs md:text-sm uppercase tracking-wider">
  103. [ RESET PASSWORD // 重置认证密钥 ]
  104. </h3>
  105. </div>
  106. <button
  107. onClick={onClose}
  108. className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
  109. >
  110. ✕
  111. </button>
  112. </div>
  113. <div className="p-5 space-y-4 bg-white text-xs">
  114. {/* Step 1: 输入邮箱 */}
  115. {step === 1 && (
  116. <form onSubmit={handleSendCode} className="space-y-4">
  117. <div className="p-3 bg-neutral-50 border-2 border-black shadow-[2px_2px_0px_#000] text-neutral-700 font-sans leading-relaxed">
  118. {t('forgot_password.step1_desc')}
  119. </div>
  120. <div className="space-y-1">
  121. <label className="block font-bold uppercase text-black">{t('forgot_password.email_label')}</label>
  122. <input
  123. type="email"
  124. required
  125. className="w-full px-3 py-2 border-2 border-black bg-neutral-50 focus:bg-white text-xs font-mono text-black outline-none shadow-[2px_2px_0px_#000]"
  126. placeholder={t('forgot_password.email_placeholder')}
  127. value={email}
  128. onChange={(e) => setEmail(e.target.value)}
  129. />
  130. </div>
  131. <button
  132. type="submit"
  133. disabled={loading}
  134. className="w-full py-2.5 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black font-black text-xs uppercase shadow-[2px_2px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 disabled:opacity-50 flex justify-center items-center gap-1.5"
  135. >
  136. {loading ? <Loader2 size={14} className="animate-spin" /> : <>[ {t('forgot_password.send_btn')} ] <ArrowRight size={14} /></>}
  137. </button>
  138. </form>
  139. )}
  140. {/* Step 2: 重置密码 */}
  141. {step === 2 && (
  142. <form onSubmit={handleSubmitReset} className="space-y-4">
  143. <div className="bg-amber-100/70 p-3 border-2 border-black shadow-[2px_2px_0px_#000] text-[11px] text-black">
  144. {t('forgot_password.code_sent_to')} <strong className="font-mono font-black">{email}</strong>
  145. </div>
  146. <div className="space-y-1">
  147. <label className="block font-bold uppercase text-black">{t('forgot_password.code_label')}</label>
  148. <div className="flex gap-2">
  149. <input
  150. type="text"
  151. required
  152. className="flex-1 border-2 border-black bg-neutral-50 focus:bg-white p-2 text-xs font-mono text-center tracking-[0.2em] font-bold text-black outline-none shadow-[2px_2px_0px_#000]"
  153. placeholder="------"
  154. maxLength={6}
  155. value={code}
  156. onChange={(e) => setCode(e.target.value)}
  157. />
  158. <button
  159. type="button"
  160. disabled={countdown > 0 || loading}
  161. onClick={handleSendCode}
  162. className="w-28 border-2 border-black bg-white hover:bg-neutral-100 text-black text-[11px] font-bold uppercase shadow-[2px_2px_0px_#000] disabled:opacity-50"
  163. >
  164. {countdown > 0 ? `${countdown}S` : t('forgot_password.resend_btn')}
  165. </button>
  166. </div>
  167. </div>
  168. <div className="space-y-1">
  169. <div className="flex items-center justify-between">
  170. <label className="block font-bold uppercase text-black">{t('forgot_password.new_password_label')}</label>
  171. <button
  172. type="button"
  173. onClick={() => setShowPassword(!showPassword)}
  174. className="text-[10px] text-black font-bold uppercase hover:underline"
  175. >
  176. {showPassword ? (t('common.hide') || 'HIDE') : (t('common.show') || 'SHOW')}
  177. </button>
  178. </div>
  179. <input
  180. type={showPassword ? "text" : "password"}
  181. required
  182. className="w-full px-3 py-2 border-2 border-black bg-neutral-50 focus:bg-white text-xs font-mono text-black outline-none shadow-[2px_2px_0px_#000]"
  183. placeholder={t('forgot_password.new_password_placeholder')}
  184. value={newPassword}
  185. onChange={(e) => setNewPassword(e.target.value)}
  186. />
  187. </div>
  188. <div className="pt-2 flex justify-end gap-2">
  189. <button
  190. type="button"
  191. onClick={() => setStep(1)}
  192. className="px-3 py-2 bg-white hover:bg-neutral-100 border-2 border-black text-xs font-bold uppercase shadow-[2px_2px_0px_#000]"
  193. >
  194. [ {t('common.back')} ]
  195. </button>
  196. <button
  197. type="submit"
  198. disabled={loading}
  199. className="flex-1 bg-amber-300 hover:bg-amber-400 text-black py-2 border-2 border-black text-xs font-black uppercase shadow-[2px_2px_0px_#000] flex justify-center items-center gap-1.5 disabled:opacity-50"
  200. >
  201. {loading ? <Loader2 size={14} className="animate-spin" /> : `[ ${t('forgot_password.reset_btn')} ]`}
  202. </button>
  203. </div>
  204. </form>
  205. )}
  206. </div>
  207. {/* Message Modal */}
  208. <MessageModal
  209. isOpen={msgModal.isOpen}
  210. title={msgModal.title}
  211. message={msgModal.message}
  212. type={msgModal.type}
  213. onClose={handleCloseMsg}
  214. />
  215. </div>
  216. </div>
  217. );
  218. }