import React, { useState, useEffect } from 'react'; import { useUser } from '../../contexts/UserContext'; import { authService } from '../../services/auth/auth.service'; import { PillIcon } from '../icons/Icons'; const AuthPage: React.FC = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [isSignUp, setIsSignUp] = useState(false); const [error, setError] = useState(''); const { login, register, loginWithOAuth } = useUser(); // State for email verification result const [verificationResult, setVerificationResult] = useState< null | 'success' | 'error' >(null); // Extract token from URL and verify email useEffect(() => { const path = window.location.pathname; const params = new URLSearchParams(window.location.search); const token = params.get('token'); if (path === '/verify-email' && token) { authService .verifyEmail(token) .then(() => setVerificationResult('success')) .catch(() => setVerificationResult('error')); } }, []); // FIX: Made the function async and added await to handle promises from login. const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (!email.trim()) { setError('Email cannot be empty.'); return; } if (!password.trim()) { setError('Password cannot be empty.'); return; } // Validate email format (allow localhost for admin) const emailRegex = /^[^\s@]+@[^\s@]+$/; // Simplified to allow any domain including localhost if (!emailRegex.test(email)) { setError('Please enter a valid email address.'); return; } if (isSignUp) { // Registration if (password.length < 6) { setError('Password must be at least 6 characters long.'); return; } if (password !== confirmPassword) { setError('Passwords do not match.'); return; } const success = await register(email, password); if (success) { setError( 'Registration successful! Please check your email for verification (demo: verification not actually sent).' ); setIsSignUp(false); // Switch back to login mode setPassword(''); setConfirmPassword(''); } else { setError('Registration failed. Email may already be in use.'); } } else { // Login const success = await login(email, password); if (!success) { setError('Login failed. Please check your email and password.'); } } }; const handleOAuthLogin = async (provider: 'google' | 'github') => { setError(''); try { // Mock OAuth data - in a real app, this would come from the OAuth provider const mockUserData = { email: provider === 'google' ? 'user@gmail.com' : 'user@github.com', username: `${provider}_user_${Date.now()}`, avatar: `https://via.placeholder.com/150?text=${provider.toUpperCase()}`, }; const success = await loginWithOAuth(provider, mockUserData); if (!success) { setError(`${provider} authentication failed. Please try again.`); } } catch { setError(`${provider} authentication failed. Please try again.`); } }; if (verificationResult) { return (
Email verified successfully! You can now sign in.
) : (Email verification failed. Please try again.
)}Sign in with your email or create an account