mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-06 11:06:21 +01:00
164 lines
4.0 KiB
JavaScript
164 lines
4.0 KiB
JavaScript
import React, { useState } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import {
|
|
Container,
|
|
Paper,
|
|
Typography,
|
|
TextField,
|
|
Button,
|
|
Box,
|
|
Alert,
|
|
} from '@mui/material';
|
|
import axios from 'axios';
|
|
|
|
const Register = () => {
|
|
const navigate = useNavigate();
|
|
const [formData, setFormData] = useState({
|
|
name: '',
|
|
email: '',
|
|
password: '',
|
|
confirmPassword: '',
|
|
});
|
|
const [error, setError] = useState('');
|
|
const [success, setSuccess] = useState('');
|
|
|
|
const handleChange = (e) => {
|
|
setFormData({
|
|
...formData,
|
|
[e.target.name]: e.target.value,
|
|
});
|
|
};
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
setSuccess('');
|
|
|
|
if (formData.password !== formData.confirmPassword) {
|
|
setError('Passwords do not match');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await axios.post('http://localhost:5000/api/users/register', {
|
|
name: formData.name,
|
|
email: formData.email,
|
|
password: formData.password,
|
|
});
|
|
|
|
setSuccess('Registration successful! Redirecting to login...');
|
|
setTimeout(() => {
|
|
navigate('/login');
|
|
}, 2000);
|
|
} catch (err) {
|
|
setError(err.response?.data?.message || 'Registration failed');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Container component="main" maxWidth="xs">
|
|
<Box
|
|
sx={{
|
|
marginTop: 8,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<Paper
|
|
elevation={3}
|
|
sx={{
|
|
padding: 4,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
alignItems: 'center',
|
|
width: '100%',
|
|
}}
|
|
>
|
|
<Typography component="h1" variant="h5" sx={{ mb: 3, fontWeight: 'bold' }}>
|
|
Register
|
|
</Typography>
|
|
|
|
{error && (
|
|
<Alert severity="error" sx={{ width: '100%', mb: 2 }}>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
{success && (
|
|
<Alert severity="success" sx={{ width: '100%', mb: 2 }}>
|
|
{success}
|
|
</Alert>
|
|
)}
|
|
|
|
<Box component="form" onSubmit={handleSubmit} sx={{ width: '100%' }}>
|
|
<TextField
|
|
margin="normal"
|
|
required
|
|
fullWidth
|
|
id="name"
|
|
label="Full Name"
|
|
name="name"
|
|
autoComplete="name"
|
|
autoFocus
|
|
value={formData.name}
|
|
onChange={handleChange}
|
|
/>
|
|
<TextField
|
|
margin="normal"
|
|
required
|
|
fullWidth
|
|
id="email"
|
|
label="Email Address"
|
|
name="email"
|
|
autoComplete="email"
|
|
value={formData.email}
|
|
onChange={handleChange}
|
|
/>
|
|
<TextField
|
|
margin="normal"
|
|
required
|
|
fullWidth
|
|
name="password"
|
|
label="Password"
|
|
type="password"
|
|
id="password"
|
|
autoComplete="new-password"
|
|
value={formData.password}
|
|
onChange={handleChange}
|
|
/>
|
|
<TextField
|
|
margin="normal"
|
|
required
|
|
fullWidth
|
|
name="confirmPassword"
|
|
label="Confirm Password"
|
|
type="password"
|
|
id="confirmPassword"
|
|
value={formData.confirmPassword}
|
|
onChange={handleChange}
|
|
/>
|
|
<Button
|
|
type="submit"
|
|
fullWidth
|
|
variant="contained"
|
|
sx={{ mt: 3, mb: 2 }}
|
|
>
|
|
Register
|
|
</Button>
|
|
<Button
|
|
fullWidth
|
|
variant="text"
|
|
onClick={() => navigate('/login')}
|
|
sx={{ mt: 1 }}
|
|
>
|
|
Already have an account? Login
|
|
</Button>
|
|
</Box>
|
|
</Paper>
|
|
</Box>
|
|
</Container>
|
|
);
|
|
};
|
|
|
|
export default Register;
|