Spaces:
Paused
Paused
File size: 7,979 Bytes
57864b1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 'use client';
/**
* 登录/注册弹窗组件
*/
import { useState } from 'react';
import { X, User, Lock, Eye, EyeOff } from 'lucide-react';
import { useAuthStore } from '@/store/authStore';
interface AuthModalProps {
onClose: () => void;
initialMode?: 'login' | 'register';
}
export default function AuthModal({ onClose, initialMode = 'login' }: AuthModalProps) {
const [mode, setMode] = useState<'login' | 'register'>(initialMode);
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [error, setError] = useState('');
const { login, register, isLoading } = useAuthStore();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
// 前端验证
if (username.trim().length < 3) {
setError('用户名至少3个字符');
return;
}
if (password.length < 6) {
setError('密码至少6位');
return;
}
if (mode === 'register' && password !== confirmPassword) {
setError('两次密码输入不一致');
return;
}
try {
if (mode === 'login') {
await login(username.trim(), password);
} else {
await register(username.trim(), password);
}
onClose();
} catch (err: any) {
setError(err.message || (mode === 'login' ? '登录失败' : '注册失败'));
}
};
const switchMode = () => {
setMode(mode === 'login' ? 'register' : 'login');
setError('');
setConfirmPassword('');
};
return (
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50 p-4" onClick={onClose}>
<div
className="bg-[#1E212B] rounded-2xl w-full max-w-sm shadow-2xl border border-[#2A2D3C] overflow-hidden"
onClick={e => e.stopPropagation()}
>
{/* Header */}
<div className="px-5 py-4 border-b border-[#2A2D3C] flex items-center justify-between">
<h3 className="text-lg font-bold text-white">
{mode === 'login' ? '登录' : '注册'}
</h3>
<button
onClick={onClose}
className="w-8 h-8 flex items-center justify-center rounded-lg bg-[#2A2D3C] hover:bg-[#35394B] text-gray-400 hover:text-white transition-colors"
>
<X size={16} />
</button>
</div>
{/* Form */}
<form onSubmit={handleSubmit} className="p-5 space-y-4">
{/* Error */}
{error && (
<div className="bg-red-500/10 border border-red-500/30 text-red-400 text-sm px-4 py-2.5 rounded-lg">
{error}
</div>
)}
{/* Username */}
<div>
<label className="text-gray-400 text-xs mb-1.5 block">用户名</label>
<div className="relative">
<User size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" />
<input
type="text"
value={username}
onChange={e => setUsername(e.target.value)}
placeholder="请输入用户名(至少3位)"
className="w-full bg-[#12141C] border border-gray-700/50 rounded-xl pl-10 pr-4 py-3 text-white text-sm placeholder-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
autoComplete="username"
autoFocus
/>
</div>
</div>
{/* Password */}
<div>
<label className="text-gray-400 text-xs mb-1.5 block">密码</label>
<div className="relative">
<Lock size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" />
<input
type={showPassword ? 'text' : 'password'}
value={password}
onChange={e => setPassword(e.target.value)}
placeholder="请输入密码(至少6位)"
className="w-full bg-[#12141C] border border-gray-700/50 rounded-xl pl-10 pr-10 py-3 text-white text-sm placeholder-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
autoComplete={mode === 'login' ? 'current-password' : 'new-password'}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-300"
>
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
</button>
</div>
</div>
{/* Confirm Password (register only) */}
{mode === 'register' && (
<div>
<label className="text-gray-400 text-xs mb-1.5 block">确认密码</label>
<div className="relative">
<Lock size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" />
<input
type={showPassword ? 'text' : 'password'}
value={confirmPassword}
onChange={e => setConfirmPassword(e.target.value)}
placeholder="请再次输入密码"
className="w-full bg-[#12141C] border border-gray-700/50 rounded-xl pl-10 pr-4 py-3 text-white text-sm placeholder-gray-600 focus:outline-none focus:border-blue-500 transition-colors"
autoComplete="new-password"
/>
</div>
</div>
)}
{/* Submit */}
<button
type="submit"
disabled={isLoading}
className="w-full bg-gradient-to-r from-blue-600 to-blue-500 hover:from-blue-500 hover:to-blue-400 text-white font-bold py-3 rounded-xl transition-all duration-200 disabled:opacity-50 shadow-lg text-sm"
>
{isLoading ? '处理中...' : (mode === 'login' ? '登录' : '注册')}
</button>
{/* Switch mode */}
<div className="text-center text-sm text-gray-500">
{mode === 'login' ? (
<>还没有账号?<button type="button" onClick={switchMode} className="text-blue-400 hover:text-blue-300 ml-1">立即注册</button></>
) : (
<>已有账号?<button type="button" onClick={switchMode} className="text-blue-400 hover:text-blue-300 ml-1">立即登录</button></>
)}
</div>
</form>
</div>
</div>
);
}
|