RealBlocks / client /src /components /Auth /RegisterForm.tsx
Sebebeb's picture
KABOM
11c0af1
Raw
History Blame Contribute Delete
6.64 kB
import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import { useAuthStore } from '../../store/authStore';
import { Blocks, Eye, EyeOff, Mail, User as UserIcon, Lock, Check } from 'lucide-react';
import GoogleButton from './GoogleButton';
export default function RegisterForm() {
const [email, setEmail] = useState('');
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [passwordError, setPasswordError] = useState('');
const [formError, setFormError] = useState('');
const { register, loading, error: storeError, clearError } = useAuthStore();
const navigate = useNavigate();
const displayError = formError || storeError;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setPasswordError('');
setFormError('');
if (password !== confirmPassword) {
setPasswordError('Passwords do not match');
return;
}
if (password.length < 8) {
setPasswordError('Password must be at least 8 characters');
return;
}
try {
await register(email, username, password);
navigate('/dashboard');
} catch (err: any) {
setFormError(err.message || 'Registration failed. Please try again.');
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-surface-950 p-4">
<div className="w-full max-w-md">
<div className="text-center mb-8">
<div className="flex items-center justify-center gap-2 mb-4">
<Blocks className="w-10 h-10 text-primary-500" />
<span className="text-3xl font-bold text-white">RealBlocks</span>
</div>
<p className="text-surface-400">Create your account</p>
</div>
<form onSubmit={handleSubmit} className="card space-y-4">
{(displayError || passwordError) && (
<div className="bg-red-500/10 border border-red-500/30 text-red-400 px-4 py-2 rounded-lg text-sm">
{passwordError || displayError}
</div>
)}
<div>
<label className="block text-sm text-surface-300 mb-1">Username</label>
<div className="relative">
<UserIcon className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-surface-400" />
<input
type="text"
value={username}
onChange={(e) => { setUsername(e.target.value); clearError(); }}
className="input pl-10"
placeholder="coolcoder"
required
minLength={3}
maxLength={30}
pattern="^[a-zA-Z0-9_]+$"
/>
</div>
</div>
<div>
<label className="block text-sm text-surface-300 mb-1">Email</label>
<div className="relative">
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-surface-400" />
<input
type="email"
value={email}
onChange={(e) => { setEmail(e.target.value); clearError(); }}
className="input pl-10"
placeholder="you@example.com"
required
/>
</div>
</div>
<div>
<label className="block text-sm text-surface-300 mb-1">Password</label>
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-surface-400" />
<input
type={showPassword ? 'text' : 'password'}
value={password}
onChange={(e) => { setPassword(e.target.value); clearError(); setPasswordError(''); }}
className="input pl-10 pr-10"
placeholder="••••••••"
required
minLength={8}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-3 top-1/2 -translate-y-1/2 text-surface-400 hover:text-white"
>
{showPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
</button>
</div>
<div className="flex gap-2 mt-1 text-xs">
<span className={`flex items-center gap-1 ${password.length >= 8 ? 'text-green-400' : 'text-surface-400'}`}>
<Check className="w-3 h-3" /> 8+ chars
</span>
<span className={`flex items-center gap-1 ${/[A-Z]/.test(password) ? 'text-green-400' : 'text-surface-400'}`}>
<Check className="w-3 h-3" /> Uppercase
</span>
<span className={`flex items-center gap-1 ${/[0-9]/.test(password) ? 'text-green-400' : 'text-surface-400'}`}>
<Check className="w-3 h-3" /> Number
</span>
</div>
</div>
<div>
<label className="block text-sm text-surface-300 mb-1">Confirm Password</label>
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-surface-400" />
<input
type={showPassword ? 'text' : 'password'}
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
className="input pl-10"
placeholder="••••••••"
required
/>
</div>
</div>
<button type="submit" disabled={loading} className="btn-primary w-full">
{loading ? 'Creating account...' : 'Create Account'}
</button>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-surface-700" />
</div>
<div className="relative flex justify-center text-xs">
<span className="bg-surface-800 px-2 text-surface-400">or continue with</span>
</div>
</div>
<GoogleButton label="Sign up with Google" />
<p className="text-center text-sm text-surface-400">
Already have an account?{' '}
<Link to="/login" className="text-primary-400 hover:text-primary-300">
Sign in
</Link>
</p>
</form>
</div>
</div>
);
}