| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 'use client';
- import { useState, useEffect } from 'react';
- import api from '@/lib/api';
- import { X, Lock, Loader2, Save, Mail, ArrowRight, KeyRound, Eye, EyeOff } from 'lucide-react';
- import { useLanguage } from '@/lib/i18n/LanguageContext';
- import MessageModal from '@/components/common/MessageModal';
- interface ForgotPasswordModalProps {
- isOpen: boolean;
- onClose: () => void;
- }
- export default function ForgotPasswordModal({ isOpen, onClose }: ForgotPasswordModalProps) {
- const { t } = useLanguage();
- const [step, setStep] = useState<1 | 2>(1);
- const [loading, setLoading] = useState(false);
- const [countdown, setCountdown] = useState(0);
-
- const [email, setEmail] = useState('');
- const [code, setCode] = useState('');
- const [newPassword, setNewPassword] = useState('');
- const [showPassword, setShowPassword] = useState(false);
- const [msgModal, setMsgModal] = useState({
- isOpen: false,
- title: '',
- message: '',
- type: 'info' as 'info' | 'error' | 'success',
- onOk: null as (() => void) | null,
- });
- const showMessage = (msg: string, type: 'info' | 'error' | 'success' = 'info', onOk?: () => void) => {
- setMsgModal({
- isOpen: true,
- title: type === 'error' ? t('common.error') : t('common.notice'),
- message: msg,
- type,
- onOk: onOk || null,
- });
- };
- const handleCloseMsg = () => {
- const callback = msgModal.onOk;
- setMsgModal((prev) => ({ ...prev, isOpen: false }));
- if (callback) callback();
- };
- useEffect(() => {
- let timer: NodeJS.Timeout;
- if (countdown > 0) {
- timer = setTimeout(() => setCountdown(c => c - 1), 1000);
- }
- return () => clearTimeout(timer);
- }, [countdown]);
- useEffect(() => {
- if (isOpen) {
- setStep(1);
- setCode('');
- setNewPassword('');
- }
- }, [isOpen]);
- const handleSendCode = async (e: React.FormEvent) => {
- e.preventDefault();
- if (!email) return showMessage(t('forgot_password.enter_email_alert'), 'error');
-
- setLoading(true);
- try {
- await api.post('/api/auth/send-reset-code', { email });
-
- showMessage(`${t('forgot_password.code_sent_alert')} ${email}`, 'success');
- setStep(2);
- setCountdown(60);
- } catch (error: any) {
- console.error(error);
- const msg = error.response?.data?.message || t('forgot_password.send_failed_default');
- showMessage(`${t('forgot_password.send_failed')}: ${msg}`, 'error');
- } finally {
- setLoading(false);
- }
- };
- const handleSubmitReset = async (e: React.FormEvent) => {
- e.preventDefault();
- if (!code) return showMessage(t('forgot_password.enter_code_alert'), 'error');
- if (!newPassword) return showMessage(t('forgot_password.enter_password_alert'), 'error');
- setLoading(true);
- try {
- await api.post('/api/auth/reset-password', {
- email,
- code,
- new_password: newPassword
- });
- showMessage(t('forgot_password.reset_success'), 'success', () => {
- onClose();
- });
- } catch (error: any) {
- console.error(error);
- const msg = error.response?.data?.message || t('forgot_password.code_error');
- showMessage(`${t('forgot_password.reset_failed')}: ${msg}`, 'error');
- } finally {
- setLoading(false);
- }
- };
- if (!isOpen) return null;
- return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4 font-mono">
- <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">
-
- {/* Header */}
- <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
- <div className="flex items-center gap-2">
- <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
- <h3 className="font-black text-black text-xs md:text-sm uppercase tracking-wider">
- [ RESET PASSWORD // 重置认证密钥 ]
- </h3>
- </div>
- <button
- onClick={onClose}
- className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
- >
- ✕
- </button>
- </div>
- <div className="p-5 space-y-4 bg-white text-xs">
- {/* Step 1: 输入邮箱 */}
- {step === 1 && (
- <form onSubmit={handleSendCode} className="space-y-4">
- <div className="p-3 bg-neutral-50 border-2 border-black shadow-[2px_2px_0px_#000] text-neutral-700 font-sans leading-relaxed">
- {t('forgot_password.step1_desc')}
- </div>
- <div className="space-y-1">
- <label className="block font-bold uppercase text-black">{t('forgot_password.email_label')}</label>
- <input
- type="email"
- required
- 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]"
- placeholder={t('forgot_password.email_placeholder')}
- value={email}
- onChange={(e) => setEmail(e.target.value)}
- />
- </div>
- <button
- type="submit"
- disabled={loading}
- 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"
- >
- {loading ? <Loader2 size={14} className="animate-spin" /> : <>[ {t('forgot_password.send_btn')} ] <ArrowRight size={14} /></>}
- </button>
- </form>
- )}
- {/* Step 2: 重置密码 */}
- {step === 2 && (
- <form onSubmit={handleSubmitReset} className="space-y-4">
- <div className="bg-amber-100/70 p-3 border-2 border-black shadow-[2px_2px_0px_#000] text-[11px] text-black">
- {t('forgot_password.code_sent_to')} <strong className="font-mono font-black">{email}</strong>
- </div>
- <div className="space-y-1">
- <label className="block font-bold uppercase text-black">{t('forgot_password.code_label')}</label>
- <div className="flex gap-2">
- <input
- type="text"
- required
- 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]"
- placeholder="------"
- maxLength={6}
- value={code}
- onChange={(e) => setCode(e.target.value)}
- />
- <button
- type="button"
- disabled={countdown > 0 || loading}
- onClick={handleSendCode}
- 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"
- >
- {countdown > 0 ? `${countdown}S` : t('forgot_password.resend_btn')}
- </button>
- </div>
- </div>
- <div className="space-y-1">
- <div className="flex items-center justify-between">
- <label className="block font-bold uppercase text-black">{t('forgot_password.new_password_label')}</label>
- <button
- type="button"
- onClick={() => setShowPassword(!showPassword)}
- className="text-[10px] text-black font-bold uppercase hover:underline"
- >
- {showPassword ? (t('common.hide') || 'HIDE') : (t('common.show') || 'SHOW')}
- </button>
- </div>
- <input
- type={showPassword ? "text" : "password"}
- required
- 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]"
- placeholder={t('forgot_password.new_password_placeholder')}
- value={newPassword}
- onChange={(e) => setNewPassword(e.target.value)}
- />
- </div>
- <div className="pt-2 flex justify-end gap-2">
- <button
- type="button"
- onClick={() => setStep(1)}
- 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]"
- >
- [ {t('common.back')} ]
- </button>
- <button
- type="submit"
- disabled={loading}
- 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"
- >
- {loading ? <Loader2 size={14} className="animate-spin" /> : `[ ${t('forgot_password.reset_btn')} ]`}
- </button>
- </div>
- </form>
- )}
- </div>
- {/* Message Modal */}
- <MessageModal
- isOpen={msgModal.isOpen}
- title={msgModal.title}
- message={msgModal.message}
- type={msgModal.type}
- onClose={handleCloseMsg}
- />
- </div>
- </div>
- );
- }
|