$(document).ready(function() { checkAuthStatus(); $('#loginForm').on('submit', function(e) { e.preventDefault(); const email = $('#login-email').val(); const password = $('#login-password').val(); const remember = $('#remember').is(':checked'); $.ajax({ url: 'login_handler.php', method: 'POST', data: { email: email, password: password }, success: function(response) { try { const result = JSON.parse(response); if (result.success) { if (remember) { localStorage.setItem('rememberedEmail', email); } else { localStorage.removeItem('rememberedEmail'); } window.location.href = result.redirect || 'catalog.php'; } else { showMessage('error', result.message || 'Ошибка авторизации'); } } catch(e) { showMessage('error', 'Ошибка обработки ответа'); } }, error: function() { showMessage('error', 'Ошибка сервера'); } }); }); function checkAuthStatus() { $.ajax({ url: 'check_auth_status.php', method: 'GET', success: function(response) { try { const result = JSON.parse(response); if (result.loggedIn) { updateUserProfile(result.user); } } catch(e) { console.error('Ошибка проверки авторизации', e); } } }); } function updateUserProfile(user) { if ($('#userEmail').length) { $('#userEmail').text(user.email); } if ($('#userName').length) { $('#userName').text(user.full_name); } } function showMessage(type, text) { const $message = $('#' + type + 'Message'); if ($message.length) { $message.text(text).fadeIn(); setTimeout(() => $message.fadeOut(), 5000); } else { alert(text); } } function checkAuth(redirectUrl) { $.ajax({ url: 'check_auth_status.php', method: 'GET', success: function(response) { try { const result = JSON.parse(response); if (result.loggedIn) { window.location.href = redirectUrl; } else { showLoginModal(redirectUrl); } } catch(e) { showLoginModal(redirectUrl); } } }); return false; } function showLoginModal(redirectUrl) { window.location.href = 'вход.php?redirect=' + encodeURIComponent(redirectUrl); } });