Register.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React, { useState } from 'react';
  2. import { Container, Typography, TextField, Button, Box, Alert, Paper } from '@mui/material';
  3. import { useNavigate } from 'react-router-dom';
  4. import { useAuth } from '../context/AuthContext';
  5. const Register = () => {
  6. const [username, setUsername] = useState('');
  7. const [email, setEmail] = useState('');
  8. const [password, setPassword] = useState('');
  9. const [confirmPassword, setConfirmPassword] = useState('');
  10. const [error, setError] = useState('');
  11. const { register } = useAuth();
  12. const navigate = useNavigate();
  13. const handleSubmit = async (e) => {
  14. e.preventDefault();
  15. setError('');
  16. if (password.length < 9) {
  17. setError('Password must be at least 9 characters long.');
  18. return;
  19. }
  20. if (password !== confirmPassword) {
  21. setError('Passwords do not match.');
  22. return;
  23. }
  24. const success = await register(username, email, password);
  25. if (success) {
  26. navigate('/login');
  27. } else {
  28. setError('Registration failed. Username or email might be taken.');
  29. }
  30. };
  31. return (
  32. <Container maxWidth="xs">
  33. <Paper elevation={3} sx={{ p: 4, mt: 8, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  34. <Typography component="h1" variant="h5">
  35. Register
  36. </Typography>
  37. {error && <Alert severity="error" sx={{ mt: 2, width: '100%' }}>{error}</Alert>}
  38. <Box component="form" onSubmit={handleSubmit} sx={{ mt: 1, width: '100%' }}>
  39. <TextField
  40. margin="normal"
  41. required
  42. fullWidth
  43. id="username"
  44. label="Username"
  45. name="username"
  46. autoComplete="username"
  47. autoFocus
  48. value={username}
  49. onChange={(e) => setUsername(e.target.value)}
  50. />
  51. <TextField
  52. margin="normal"
  53. required
  54. fullWidth
  55. id="email"
  56. label="Email Address"
  57. name="email"
  58. autoComplete="email"
  59. value={email}
  60. onChange={(e) => setEmail(e.target.value)}
  61. />
  62. <TextField
  63. margin="normal"
  64. required
  65. fullWidth
  66. name="password"
  67. label="Password"
  68. type="password"
  69. id="password"
  70. autoComplete="new-password"
  71. value={password}
  72. onChange={(e) => setPassword(e.target.value)}
  73. />
  74. <TextField
  75. margin="normal"
  76. required
  77. fullWidth
  78. name="confirmPassword"
  79. label="Confirm Password"
  80. type="password"
  81. id="confirmPassword"
  82. autoComplete="new-password"
  83. value={confirmPassword}
  84. onChange={(e) => setConfirmPassword(e.target.value)}
  85. />
  86. <Button
  87. type="submit"
  88. fullWidth
  89. variant="contained"
  90. sx={{ mt: 3, mb: 2 }}
  91. >
  92. Register
  93. </Button>
  94. </Box>
  95. </Paper>
  96. </Container>
  97. );
  98. };
  99. export default Register;