File size: 8,064 Bytes
5008578
1d8cf7a
2ff64f6
82ccb3f
9288c82
5008578
 
 
7a97213
 
5008578
 
 
 
 
 
82ccb3f
5008578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ccb3f
5008578
 
 
 
 
82ccb3f
5008578
 
 
 
 
82ccb3f
5008578
 
 
 
82ccb3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5008578
 
82ccb3f
5008578
82ccb3f
5008578
82ccb3f
5008578
82ccb3f
34f34a3
 
 
 
 
 
 
 
 
 
 
 
 
5e77c6e
34f34a3
 
 
 
82ccb3f
5008578
 
 
 
 
 
 
 
82ccb3f
 
5008578
 
9288c82
 
5008578
 
7a97213
5008578
 
 
2ff64f6
5008578
 
 
 
82ccb3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5008578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ccb3f
5008578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ccb3f
5008578
 
 
 
 
82ccb3f
5008578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ccb3f
 
5008578
82ccb3f
5008578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ccb3f
 
5008578
 
 
 
 
 
9288c82
 
 
 
 
 
5008578
 
9288c82
 
 
 
5008578
 
 
 
2ff64f6
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
import React, { useState } from 'react';
import { Eye, EyeOff, Mail, Lock, ArrowRight } from 'lucide-react';
import { useAppConfig } from '../contexts/AppConfigContext';
import { persistAuth, getApiBaseUrl } from '../utils/authStorage';
import CopyrightNotice from './CopyrightNotice';
import '../styles/Login.css';

const Login = ({ onNavigateToSignup, onNavigateToHome }) => {
  const { config, resolveIcon } = useAppConfig();
  const LogoIcon = resolveIcon(config?.app?.logo_icon || 'BookOpen');
  const [showPassword, setShowPassword] = useState(false);
  const [formData, setFormData] = useState({
    email: '',
    password: ''
  });
  const [isLoading, setIsLoading] = useState(false);
  const [isGuestLoading, setIsGuestLoading] = useState(false);
  const [errors, setErrors] = useState({});

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    setFormData(prev => ({
      ...prev,
      [name]: value
    }));
    if (errors[name]) {
      setErrors(prev => ({
        ...prev,
        [name]: ''
      }));
    }
  };

  const validateForm = () => {
    const newErrors = {};

    if (!formData.email) {
      newErrors.email = 'Email is required';
    } else if (!/\S+@\S+\.\S+/.test(formData.email)) {
      newErrors.email = 'Please enter a valid email address';
    }

    if (!formData.password) {
      newErrors.password = 'Password is required';
    } else if (formData.password.length < 6) {
      newErrors.password = 'Password must be at least 6 characters';
    }

    setErrors(newErrors);
    return Object.keys(newErrors).length === 0;
  };

  const handleGuestContinue = async () => {
    setIsGuestLoading(true);
    setErrors({});
    try {
      const response = await fetch(`${getApiBaseUrl()}/auth/guest`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
      });
      const data = await response.json();
      if (response.ok) {
        persistAuth(data.user, data.access_token);
        onNavigateToHome?.(data.user, data.access_token);
      } else {
        setErrors({ submit: data.detail || 'Could not start a guest session.' });
      }
    } catch (error) {
      console.error('Guest login error:', error);
      setErrors({ submit: 'Could not start a guest session. Please try again.' });
    } finally {
      setIsGuestLoading(false);
    }
  };

  const handleSubmit = async (e) => {
    e.preventDefault();

    if (!validateForm()) return;

    setIsLoading(true);

    try {
      const response = await fetch(`${getApiBaseUrl()}/auth/login`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          email: formData.email,
          password: formData.password
        }),
      });

      const data = await response.json();

      if (response.ok) {
        persistAuth(data.user, data.access_token);
        onNavigateToHome?.(data.user, data.access_token);
      } else {
        setErrors({ submit: data.detail || 'Login failed. Please try again.' });
      }

    } catch (error) {
      console.error('Login error:', error);
      setErrors({ submit: 'Login failed. Please try again.' });
    } finally {
      setIsLoading(false);
    }
  };

  const busy = isLoading || isGuestLoading;

  return (
    <div className="login-page">
      <div className="login-content">
        <div className="login-container">
        <div className="login-header">
          <div className="logo-container">
            <LogoIcon className="logo-icon" />
          </div>
          <h1 className="login-title">Welcome Back</h1>
          <p className="login-subtitle">
            {config?.login?.subtitle || 'Sign in to continue'}
          </p>
        </div>

        <div className="login-form-container">
          <button
            type="button"
            className={`guest-continue-btn ${isGuestLoading ? 'loading' : ''}`}
            onClick={handleGuestContinue}
            disabled={busy}
          >
            {isGuestLoading ? 'Starting guest mode…' : 'Try without an account'}
          </button>
          <p className="guest-continue-hint">
            Explore the advisors first. Create an account later if you want to save your training chats.
          </p>
          <div className="login-divider" aria-hidden="true">
            <span>or sign in</span>
          </div>

          <form onSubmit={handleSubmit} className="login-form">
            <div className="form-group">
              <label htmlFor="email" className="form-label">
                Email Address
              </label>
              <div className="input-container">
                <Mail className="input-icon" />
                <input
                  type="email"
                  id="email"
                  name="email"
                  value={formData.email}
                  onChange={handleInputChange}
                  className={`form-input ${errors.email ? 'error' : ''}`}
                  placeholder="Enter your email"
                  disabled={busy}
                />
              </div>
              {errors.email && (
                <span className="error-message">{errors.email}</span>
              )}
            </div>

            <div className="form-group">
              <label htmlFor="password" className="form-label">
                Password
              </label>
              <div className="input-container">
                <Lock className="input-icon" />
                <input
                  type={showPassword ? 'text' : 'password'}
                  id="password"
                  name="password"
                  value={formData.password}
                  onChange={handleInputChange}
                  className={`form-input ${errors.password ? 'error' : ''}`}
                  placeholder="Enter your password"
                  disabled={busy}
                />
                <button
                  type="button"
                  onClick={() => setShowPassword(!showPassword)}
                  className="password-toggle"
                  disabled={busy}
                >
                  {showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
                </button>
              </div>
              {errors.password && (
                <span className="error-message">{errors.password}</span>
              )}
            </div>

            <div className="form-actions">
              <button type="button" className="forgot-password">
                Forgot your password?
              </button>
            </div>

            {errors.submit && (
              <div className="submit-error">
                {errors.submit}
              </div>
            )}

            <button
              type="submit"
              className={`submit-btn ${isLoading ? 'loading' : ''}`}
              disabled={busy}
            >
              {isLoading ? (
                <>
                  <div className="loading-spinner"></div>
                  Signing In...
                </>
              ) : (
                <>
                  Sign In
                  <ArrowRight size={16} />
                </>
              )}
            </button>
          </form>
        </div>

        <div className="login-footer">
          <p>
            Don't have an account?{' '}
            <button
              type="button"
              className="link-btn"
              onClick={onNavigateToSignup}
            >
              Sign up here
            </button>
          </p>
          <div className="login-powered-by">
            <a href="https://neon.ai" target="_blank" rel="noopener noreferrer" className="footer-neon-link">
              <img src="/neon-logo.png" alt="" className="footer-neon-logo" />
              Powered by Neon.ai
            </a>
          </div>
        </div>
      </div>
      </div>
      <footer className="login-page-footer">
        <CopyrightNotice className="login-page-copyright" />
      </footer>
    </div>
  );
};

export default Login;