Spaces:
Running
Running
File size: 6,284 Bytes
aa06156 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import { toast } from 'react-toastify';
export default function Register() {
const [formData, setFormData] = useState({
username: '',
email: '',
fullName: '',
password: '',
confirmPassword: ''
});
const [loading, setLoading] = useState(false);
const { register } = useAuth();
const navigate = useNavigate();
const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
});
};
const handleSubmit = async (e) => {
e.preventDefault();
if (formData.password !== formData.confirmPassword) {
toast.error('رمز عبور و تکرار آن مطابقت ندارند');
return;
}
setLoading(true);
try {
await register({
username: formData.username,
email: formData.email,
password: formData.password,
fullName: formData.fullName
});
toast.success('ثبتنام با موفقیت انجام شد');
navigate('/dashboard');
} catch (err) {
toast.error(err.message || 'خطا در ثبتنام');
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
<div className="max-w-md w-full space-y-8 bg-white p-8 rounded-lg shadow-md">
<div className="text-center">
<h2 className="mt-6 text-3xl font-extrabold text-gray-900">ثبتنام در پایتون مستری</h2>
</div>
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
<div className="rounded-md shadow-sm space-y-4">
<div>
<label htmlFor="fullName" className="block text-sm font-medium text-gray-700 mb-1">
نام کامل
</label>
<input
id="fullName"
name="fullName"
type="text"
required
className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-primary focus:border-primary focus:z-10 sm:text-sm"
placeholder="نام و نام خانوادگی"
value={formData.fullName}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="username" className="block text-sm font-medium text-gray-700 mb-1">
نام کاربری
</label>
<input
id="username"
name="username"
type="text"
required
className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-primary focus:border-primary focus:z-10 sm:text-sm"
placeholder="نام کاربری دلخواه"
value={formData.username}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
آدرس ایمیل
</label>
<input
id="email"
name="email"
type="email"
required
className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-primary focus:border-primary focus:z-10 sm:text-sm"
placeholder="example@domain.com"
value={formData.email}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
رمز عبور
</label>
<input
id="password"
name="password"
type="password"
required
className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-primary focus:border-primary focus:z-10 sm:text-sm"
placeholder="••••••••"
value={formData.password}
onChange={handleChange}
/>
</div>
<div>
<label htmlFor="confirmPassword" className="block text-sm font-medium text-gray-700 mb-1">
تکرار رمز عبور
</label>
<input
id="confirmPassword"
name="confirmPassword"
type="password"
required
className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-primary focus:border-primary focus:z-10 sm:text-sm"
placeholder="••••••••"
value={formData.confirmPassword}
onChange={handleChange}
/>
</div>
</div>
<div>
<button
type="submit"
disabled={loading}
className={`group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-primary hover:bg-primary-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary ${loading ? 'opacity-70 cursor-not-allowed' : ''}`}
>
{loading ? 'در حال ثبتنام...' : 'ثبتنام'}
</button>
</div>
</form>
<div className="text-center text-sm">
<p className="text-gray-600">
قبلا ثبتنام کردهاید؟{' '}
<a href="/login" className="font-medium text-primary hover:text-primary-600">
ورود به حساب کاربری
</a>
</p>
</div>
</div>
</div>
);
} |