File size: 11,886 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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;