File size: 8,936 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 | import React, { useState, useEffect } from 'react';
import { authService, testCors } from './services/api';
import Dashboard from './components/Dashboard/Dashboard';
function App() {
const [user, setUser] = useState(null);
const [isLogin, setIsLogin] = useState(true);
const [formData, setFormData] = useState({
username: '',
email: '',
password: '',
displayName: ''
});
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [corsWorking, setCorsWorking] = useState(false);
// Check if user is already logged in (from localStorage)
useEffect(() => {
testCorsConnection();
checkExistingAuth();
}, []);
const checkExistingAuth = async () => {
const token = localStorage.getItem('token');
const userData = localStorage.getItem('user');
if (token && userData) {
try {
// Verify token is still valid by calling /api/auth/me
const response = await authService.getMe();
setUser(response.user);
} catch (error) {
// Token is invalid, clear storage
localStorage.removeItem('token');
localStorage.removeItem('user');
console.log('Session expired, please login again');
}
}
};
const testCorsConnection = async () => {
try {
const result = await testCors();
setCorsWorking(true);
console.log('CORS test passed:', result);
} catch (error) {
setCorsWorking(false);
console.error('CORS test failed:', error);
}
};
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
setError('');
try {
let response;
if (isLogin) {
// Login
response = await authService.login({
email: formData.email,
password: formData.password
});
} else {
// Register
response = await authService.register(formData);
}
// Set the user state and store in localStorage
setUser(response.user);
localStorage.setItem('token', response.token);
localStorage.setItem('user', JSON.stringify(response.user));
// Clear form
setFormData({
username: '',
email: '',
password: '',
displayName: ''
});
} catch (error) {
console.error('Auth error details:', error);
setError(error.error || 'Something went wrong! Please check the console for details.');
} finally {
setLoading(false);
}
};
const handleLogout = () => {
setUser(null);
localStorage.removeItem('token');
localStorage.removeItem('user');
};
const handleChange = (e) => {
setFormData(prev => ({
...prev,
[e.target.name]: e.target.value
}));
setError('');
};
// If user is logged in, show the Dashboard
if (user) {
return <Dashboard user={user} onLogout={handleLogout} />;
}
// Otherwise, show the login/signup form
return (
<div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4">
<div className="max-w-md w-full bg-white rounded-2xl shadow-xl overflow-hidden">
{/* Header */}
<div className="bg-gradient-to-r from-blue-600 to-indigo-700 p-8 text-center">
<h1 className="text-3xl font-bold text-white mb-2">YSNRFD Messenger</h1>
<p className="text-blue-100">Connect with anyone, anywhere</p>
</div>
{/* Form */}
<div className="p-8">
{/* Connection Status */}
<div className={`mb-4 p-3 rounded-lg text-center text-sm ${
corsWorking
? 'bg-green-50 text-green-700 border border-green-200'
: 'bg-yellow-50 text-yellow-700 border border-yellow-200'
}`}>
{corsWorking ? (
<span>✅ Backend connection working!</span>
) : (
<span>⚠️ Checking backend connection...</span>
)}
</div>
{/* Toggle */}
<div className="flex mb-6 bg-gray-100 rounded-lg p-1">
<button
type="button"
onClick={() => setIsLogin(true)}
className={`flex-1 py-2 px-4 rounded-md transition-colors ${
isLogin ? 'bg-white shadow-sm text-blue-600' : 'text-gray-600'
}`}
>
Sign In
</button>
<button
type="button"
onClick={() => setIsLogin(false)}
className={`flex-1 py-2 px-4 rounded-md transition-colors ${
!isLogin ? 'bg-white shadow-sm text-blue-600' : 'text-gray-600'
}`}
>
Sign Up
</button>
</div>
{/* Error Message */}
{error && (
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg">
<p className="text-red-600 text-sm font-medium">Error: {error}</p>
</div>
)}
<form onSubmit={handleSubmit} className="space-y-4">
{!isLogin && (
<>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Username
</label>
<input
type="text"
name="username"
value={formData.username}
onChange={handleChange}
required
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Choose a username"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Display Name
</label>
<input
type="text"
name="displayName"
value={formData.displayName}
onChange={handleChange}
required
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Your display name"
/>
</div>
</>
)}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleChange}
required
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Enter your email"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input
type="password"
name="password"
value={formData.password}
onChange={handleChange}
required
minLength="6"
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Enter your password"
/>
</div>
<button
type="submit"
disabled={loading || !corsWorking}
className="w-full bg-blue-600 text-white py-3 px-4 rounded-lg hover:bg-blue-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? (
<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>
{isLogin ? 'Signing In...' : 'Creating Account...'}
</div>
) : (
isLogin ? 'Sign In' : 'Create Account'
)}
</button>
</form>
{/* Demo Note */}
<div className="mt-6 p-4 bg-blue-50 rounded-lg">
<p className="text-sm text-blue-700 text-center">
<strong>Welcome to YSNRFD Messenger!</strong><br />
After login, you'll see the full messaging interface
</p>
</div>
</div>
</div>
</div>
);
}
export default App; |