text-dataset-tiny-code-script-py-format / ysnrfd_messenger /BACKUP2 /frontend /src /components /Auth /Register.jsx
| import React, { useState, useEffect } from 'react'; | |
| import { Link, useNavigate } from 'react-router-dom'; | |
| import { Eye, EyeOff, Mail, Lock, User, MessageCircle } from 'lucide-react'; | |
| import { useAuth } from '../../contexts/AuthContext'; | |
| import { useTheme } from '../../contexts/ThemeContext'; | |
| const Register = () => { | |
| const [formData, setFormData] = useState({ | |
| username: '', | |
| email: '', | |
| password: '', | |
| displayName: '', | |
| confirmPassword: '' | |
| }); | |
| const [showPassword, setShowPassword] = useState(false); | |
| const [showConfirmPassword, setShowConfirmPassword] = useState(false); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [validationErrors, setValidationErrors] = useState({}); | |
| const { register, error, clearError } = useAuth(); | |
| const { theme, toggleTheme } = useTheme(); | |
| const navigate = useNavigate(); | |
| useEffect(() => { | |
| clearError(); | |
| }, []); | |
| const validateForm = () => { | |
| const errors = {}; | |
| if (formData.username.length < 3) { | |
| errors.username = 'Username must be at least 3 characters'; | |
| } | |
| if (!formData.email.match(/^\S+@\S+\.\S+$/)) { | |
| errors.email = 'Please enter a valid email address'; | |
| } | |
| if (formData.password.length < 6) { | |
| errors.password = 'Password must be at least 6 characters'; | |
| } | |
| if (formData.password !== formData.confirmPassword) { | |
| errors.confirmPassword = 'Passwords do not match'; | |
| } | |
| if (!formData.displayName.trim()) { | |
| errors.displayName = 'Display name is required'; | |
| } | |
| setValidationErrors(errors); | |
| return Object.keys(errors).length === 0; | |
| }; | |
| const handleChange = (e) => { | |
| const { name, value } = e.target; | |
| setFormData(prev => ({ | |
| ...prev, | |
| [name]: value | |
| })); | |
| // Clear validation error for this field | |
| if (validationErrors[name]) { | |
| setValidationErrors(prev => ({ | |
| ...prev, | |
| [name]: '' | |
| })); | |
| } | |
| if (error) clearError(); | |
| }; | |
| const handleSubmit = async (e) => { | |
| e.preventDefault(); | |
| if (!validateForm()) { | |
| return; | |
| } | |
| setIsLoading(true); | |
| try { | |
| const { confirmPassword, ...registerData } = formData; | |
| await register(registerData); | |
| navigate('/'); | |
| } catch (error) { | |
| console.error('Registration error:', error); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }; | |
| return ( | |
| <div className="min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900 py-12 px-4 sm:px-6 lg:px-8"> | |
| <div className="max-w-md w-full space-y-8"> | |
| {/* Header */} | |
| <div className="text-center"> | |
| <div className="flex items-center justify-center space-x-3 mb-6"> | |
| <MessageCircle className="h-12 w-12 text-primary-500" /> | |
| <div> | |
| <h1 className="text-3xl font-bold text-gray-900 dark:text-white">YSNRFD</h1> | |
| <p className="text-sm text-gray-600 dark:text-gray-400">Messenger</p> | |
| </div> | |
| </div> | |
| <h2 className="text-2xl font-bold text-gray-900 dark:text-white"> | |
| Create your account | |
| </h2> | |
| <p className="mt-2 text-sm text-gray-600 dark:text-gray-400"> | |
| Or{' '} | |
| <Link | |
| to="/login" | |
| className="font-medium text-primary-500 hover:text-primary-600 transition-colors" | |
| > | |
| sign in to existing account | |
| </Link> | |
| </p> | |
| </div> | |
| {/* Theme Toggle */} | |
| <div className="flex justify-center"> | |
| <button | |
| onClick={toggleTheme} | |
| className="p-2 rounded-lg bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-300 hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors" | |
| > | |
| {theme === 'dark' ? '🌙' : '☀️'} | |
| </button> | |
| </div> | |
| {/* Error Message */} | |
| {error && ( | |
| <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4"> | |
| <p className="text-sm text-red-600 dark:text-red-400">{error}</p> | |
| </div> | |
| )} | |
| {/* Registration Form */} | |
| <form className="mt-8 space-y-6" onSubmit={handleSubmit}> | |
| <div className="space-y-4"> | |
| {/* Username Field */} | |
| <div> | |
| <label htmlFor="username" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> | |
| Username | |
| </label> | |
| <div className="relative"> | |
| <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"> | |
| <User className="h-5 w-5 text-gray-400" /> | |
| </div> | |
| <input | |
| id="username" | |
| name="username" | |
| type="text" | |
| autoComplete="username" | |
| required | |
| value={formData.username} | |
| onChange={handleChange} | |
| className={`input-field pl-10 ${validationErrors.username ? 'border-red-500' : ''}`} | |
| placeholder="Choose a username" | |
| /> | |
| </div> | |
| {validationErrors.username && ( | |
| <p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.username}</p> | |
| )} | |
| </div> | |
| {/* Display Name Field */} | |
| <div> | |
| <label htmlFor="displayName" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> | |
| Display Name | |
| </label> | |
| <input | |
| id="displayName" | |
| name="displayName" | |
| type="text" | |
| autoComplete="name" | |
| required | |
| value={formData.displayName} | |
| onChange={handleChange} | |
| className={`input-field ${validationErrors.displayName ? 'border-red-500' : ''}`} | |
| placeholder="Enter your display name" | |
| /> | |
| {validationErrors.displayName && ( | |
| <p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.displayName}</p> | |
| )} | |
| </div> | |
| {/* Email Field */} | |
| <div> | |
| <label htmlFor="email" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> | |
| Email Address | |
| </label> | |
| <div className="relative"> | |
| <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"> | |
| <Mail className="h-5 w-5 text-gray-400" /> | |
| </div> | |
| <input | |
| id="email" | |
| name="email" | |
| type="email" | |
| autoComplete="email" | |
| required | |
| value={formData.email} | |
| onChange={handleChange} | |
| className={`input-field pl-10 ${validationErrors.email ? 'border-red-500' : ''}`} | |
| placeholder="Enter your email" | |
| /> | |
| </div> | |
| {validationErrors.email && ( | |
| <p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.email}</p> | |
| )} | |
| </div> | |
| {/* Password Field */} | |
| <div> | |
| <label htmlFor="password" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> | |
| Password | |
| </label> | |
| <div className="relative"> | |
| <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"> | |
| <Lock className="h-5 w-5 text-gray-400" /> | |
| </div> | |
| <input | |
| id="password" | |
| name="password" | |
| type={showPassword ? 'text' : 'password'} | |
| autoComplete="new-password" | |
| required | |
| value={formData.password} | |
| onChange={handleChange} | |
| className={`input-field pl-10 pr-10 ${validationErrors.password ? 'border-red-500' : ''}`} | |
| placeholder="Create a password" | |
| /> | |
| <button | |
| type="button" | |
| className="absolute inset-y-0 right-0 pr-3 flex items-center" | |
| onClick={() => setShowPassword(!showPassword)} | |
| > | |
| {showPassword ? ( | |
| <EyeOff className="h-5 w-5 text-gray-400 hover:text-gray-600" /> | |
| ) : ( | |
| <Eye className="h-5 w-5 text-gray-400 hover:text-gray-600" /> | |
| )} | |
| </button> | |
| </div> | |
| {validationErrors.password && ( | |
| <p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.password}</p> | |
| )} | |
| </div> | |
| {/* Confirm Password Field */} | |
| <div> | |
| <label htmlFor="confirmPassword" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> | |
| Confirm Password | |
| </label> | |
| <div className="relative"> | |
| <div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"> | |
| <Lock className="h-5 w-5 text-gray-400" /> | |
| </div> | |
| <input | |
| id="confirmPassword" | |
| name="confirmPassword" | |
| type={showConfirmPassword ? 'text' : 'password'} | |
| autoComplete="new-password" | |
| required | |
| value={formData.confirmPassword} | |
| onChange={handleChange} | |
| className={`input-field pl-10 pr-10 ${validationErrors.confirmPassword ? 'border-red-500' : ''}`} | |
| placeholder="Confirm your password" | |
| /> | |
| <button | |
| type="button" | |
| className="absolute inset-y-0 right-0 pr-3 flex items-center" | |
| onClick={() => setShowConfirmPassword(!showConfirmPassword)} | |
| > | |
| {showConfirmPassword ? ( | |
| <EyeOff className="h-5 w-5 text-gray-400 hover:text-gray-600" /> | |
| ) : ( | |
| <Eye className="h-5 w-5 text-gray-400 hover:text-gray-600" /> | |
| )} | |
| </button> | |
| </div> | |
| {validationErrors.confirmPassword && ( | |
| <p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.confirmPassword}</p> | |
| )} | |
| </div> | |
| </div> | |
| {/* Submit Button */} | |
| <button | |
| type="submit" | |
| disabled={isLoading} | |
| className="w-full btn-primary py-3 px-4 text-base font-medium disabled:opacity-50 disabled:cursor-not-allowed" | |
| > | |
| {isLoading ? ( | |
| <div className="flex items-center justify-center"> | |
| <div className="animate-spin rounded-full h-5 w-5 border-b-2 border-white mr-2"></div> | |
| Creating account... | |
| </div> | |
| ) : ( | |
| 'Create Account' | |
| )} | |
| </button> | |
| {/* Terms Notice */} | |
| <div className="text-center"> | |
| <p className="text-xs text-gray-500 dark:text-gray-400"> | |
| By creating an account, you agree to our Terms of Service and Privacy Policy | |
| </p> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| ); | |
| }; | |
| export default Register; |