152 lines
5.4 KiB
TypeScript
152 lines
5.4 KiB
TypeScript
import { useState } from 'react'
|
|
import { useNavigate } from 'react-router-dom'
|
|
import { useAuth } from '../hooks/useAuth'
|
|
import './Login.css'
|
|
|
|
function Register() {
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [confirmPassword, setConfirmPassword] = useState('')
|
|
const [error, setError] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const { register } = useAuth()
|
|
const navigate = useNavigate()
|
|
|
|
const validateEmail = (email: string) => {
|
|
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
return re.test(email)
|
|
}
|
|
|
|
const getPasswordStrength = (password: string): { strength: string; color: string } => {
|
|
if (password.length === 0) return { strength: '', color: '' }
|
|
if (password.length < 6) return { strength: 'Weak', color: '#ff6b6b' }
|
|
if (password.length < 10) return { strength: 'Medium', color: '#ffd93d' }
|
|
return { strength: 'Strong', color: '#51cf66' }
|
|
}
|
|
|
|
const passwordStrength = getPasswordStrength(password)
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
setError('')
|
|
|
|
// Validation
|
|
if (!validateEmail(email)) {
|
|
setError('Please enter a valid email address')
|
|
return
|
|
}
|
|
|
|
if (password.length < 6) {
|
|
setError('Password must be at least 6 characters')
|
|
return
|
|
}
|
|
|
|
if (password !== confirmPassword) {
|
|
setError('Passwords do not match')
|
|
return
|
|
}
|
|
|
|
setLoading(true)
|
|
|
|
try {
|
|
await register(email, password)
|
|
// Navigate to character selection after successful registration
|
|
navigate('/characters')
|
|
} catch (err: any) {
|
|
setError(err.response?.data?.detail || 'Registration failed')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="login-container">
|
|
<div className="login-card">
|
|
<h1>Create Account</h1>
|
|
<p className="login-subtitle">Join the survivors in the wasteland</p>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="form-group">
|
|
<label htmlFor="email">Email Address</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
placeholder="your.email@example.com"
|
|
required
|
|
disabled={loading}
|
|
autoComplete="email"
|
|
/>
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
<label htmlFor="password">Password</label>
|
|
<input
|
|
type="password"
|
|
id="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
placeholder="At least 6 characters"
|
|
required
|
|
disabled={loading}
|
|
autoComplete="new-password"
|
|
/>
|
|
{password && (
|
|
<div className="password-strength">
|
|
<span style={{ color: passwordStrength.color }}>
|
|
{passwordStrength.strength}
|
|
</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
<label htmlFor="confirmPassword">Confirm Password</label>
|
|
<input
|
|
type="password"
|
|
id="confirmPassword"
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
placeholder="Re-enter your password"
|
|
required
|
|
disabled={loading}
|
|
autoComplete="new-password"
|
|
/>
|
|
</div>
|
|
|
|
{error && <div className="error">{error}</div>}
|
|
|
|
<button type="submit" className="button-primary" disabled={loading}>
|
|
{loading ? 'Creating Account...' : 'Create Account'}
|
|
</button>
|
|
</form>
|
|
|
|
<div className="login-toggle">
|
|
<button
|
|
type="button"
|
|
className="button-link"
|
|
onClick={() => navigate('/login')}
|
|
disabled={loading}
|
|
>
|
|
Already have an account? Login
|
|
</button>
|
|
</div>
|
|
|
|
<div className="login-toggle">
|
|
<button
|
|
type="button"
|
|
className="button-link"
|
|
onClick={() => navigate('/')}
|
|
disabled={loading}
|
|
>
|
|
← Back to Home
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default Register
|