Files
whats-the-point/frontend/src/pages/Register.jsx

98 lines
3.3 KiB
JavaScript

import React, { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import toast from 'react-hot-toast';
import { useAuth } from '../AuthContext';
export default function Register() {
const [email, setEmail] = useState('');
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [loading, setLoading] = useState(false);
const { register } = useAuth();
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
await register(email, username, password);
toast.success('Account created successfully!');
navigate('/challenges');
} catch (err) {
toast.error(err.message || 'Registration failed');
} finally {
setLoading(false);
}
};
return (
<div style={{ minHeight: '100vh', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '1rem' }}>
<div className="card" style={{ width: '100%', maxWidth: '400px' }}>
<h1 style={{ marginBottom: '2rem', textAlign: 'center' }}>Create Account</h1>
<form onSubmit={handleSubmit}>
<div className="form-group">
<label>Email</label>
<input
type="email"
className="input"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<div className="form-group">
<label>Username</label>
<input
type="text"
className="input"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
</div>
<div className="form-group">
<label>Password</label>
<div style={{ position: 'relative' }}>
<input
type={showPassword ? 'text' : 'password'}
className="input"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={6}
style={{ paddingRight: '2.5rem' }}
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
style={{
position: 'absolute',
right: '0.5rem',
top: '50%',
transform: 'translateY(-50%)',
background: 'none',
border: 'none',
cursor: 'pointer',
padding: '0.25rem',
color: 'var(--text-muted)',
fontSize: '0.875rem'
}}
>
{showPassword ? '👁️' : '👁️‍🗨️'}
</button>
</div>
</div>
<button type="submit" className="btn btn-primary" style={{ width: '100%', marginTop: '1rem' }} disabled={loading}>
{loading ? 'Creating account...' : 'Register'}
</button>
</form>
<p style={{ marginTop: '1.5rem', textAlign: 'center', color: 'var(--text-muted)' }}>
Already have an account? <Link to="/login" style={{ color: 'var(--primary)' }}>Login</Link>
</p>
</div>
</div>
);
}