import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, ArrowLeft } from 'lucide-react'; import { __ } from '@/lib/i18n'; export function Login() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [branding, setBranding] = React.useState({ logo: '', logoDark: '', storeName: 'WooNooW', tagline: '', }); const [isDark, setIsDark] = React.useState(false); const navigate = useNavigate(); // Detect dark mode React.useEffect(() => { const checkDarkMode = () => { setIsDark(document.documentElement.classList.contains('dark')); }; checkDarkMode(); const observer = new MutationObserver(checkDarkMode); observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); return () => observer.disconnect(); }, []); // Fetch branding (public endpoint - no auth required) useEffect(() => { fetch((window.WNW_CONFIG?.restUrl || '') + '/store/branding') .then(res => res.json()) .then(data => { setBranding({ logo: data.store_logo || '', logoDark: data.store_logo_dark || '', storeName: data.store_name || 'WooNooW', tagline: data.store_tagline || '', }); }) .catch(err => console.error('Failed to load branding:', err)); }, []); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(''); try { const response = await fetch((window.WNW_CONFIG?.restUrl || '') + '/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ username, password }), }); const data = await response.json(); if (response.ok && data.success) { // CRITICAL: Reload page to get fresh cookies and nonce from PHP // wp_signon sets cookies but we need PHP to render with proper auth state window.location.href = window.location.origin + window.location.pathname; } else { setError(data.message || __('Invalid username or password')); } } catch (err) { console.error('Login error:', err); setError(__('Login failed. Please try again.')); } finally { setIsLoading(false); } }; return (
{branding.tagline}
)}{__('Sign in to your admin dashboard')}