Новый функционал входа и выхода в мобильном меню
This commit is contained in:
parent
f3dbfb3587
commit
2f57bf91ef
5 changed files with 196 additions and 11 deletions
|
|
@ -44,8 +44,8 @@ import { COMPANY } from "@constants";
|
|||
|
||||
<div class="header-actions" id="auth-section"></div>
|
||||
|
||||
<div class="mobile-actions">
|
||||
<a href={`tel:${COMPANY.phoneClean}`} class="mobile-phone-btn" aria-label="Позони<EFBFBD><EFBFBD>ь: {COMPANY.phone}">
|
||||
<div class="mobile-actions">
|
||||
<a href={`tel:${COMPANY.phoneClean}`} class="mobile-phone-btn" aria-label="Позониь: {COMPANY.phone}">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
|
|
@ -395,8 +395,10 @@ import { COMPANY } from "@constants";
|
|||
localStorage.removeItem('user');
|
||||
localStorage.removeItem('pocketbase_auth');
|
||||
document.querySelector('.header-right')?.classList.remove('auth-active');
|
||||
// Редирект на текущую страницу
|
||||
window.location.href = window.location.pathname + window.location.search + window.location.hash;
|
||||
// Редирект на текущую страницу с параметром loggedout
|
||||
const currentUrl = window.location.pathname + window.location.search + window.location.hash;
|
||||
const separator = currentUrl.includes('?') ? '&' : '?';
|
||||
window.location.href = currentUrl + separator + 'loggedout=true';
|
||||
});
|
||||
|
||||
// Добавляем класс для изменения порядка
|
||||
|
|
@ -415,6 +417,19 @@ import { COMPANY } from "@constants";
|
|||
document.querySelector('.header-right')?.classList.remove('auth-active');
|
||||
}
|
||||
|
||||
// Проверка параметра loggedout и показ Toast
|
||||
function checkLogoutToast() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
if (urlParams.get('loggedout') === 'true') {
|
||||
// Очистить URL без перезагрузки
|
||||
window.history.replaceState({}, '', window.location.pathname + window.location.hash);
|
||||
// Показать Toast
|
||||
setTimeout(() => {
|
||||
window.toast?.show('Вы вышли из системы', 'info', 3000);
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
|
||||
// Стили
|
||||
const authStyle = `
|
||||
.header-right.auth-active {
|
||||
|
|
@ -485,6 +500,8 @@ import { COMPANY } from "@constants";
|
|||
document.head.appendChild(styleEl);
|
||||
|
||||
initAuth();
|
||||
checkLogoutToast();
|
||||
|
||||
const animatedElements = document.querySelectorAll(".animate-load");
|
||||
|
||||
animatedElements.forEach((el) => {
|
||||
|
|
|
|||
|
|
@ -72,14 +72,14 @@ import { NAV_LINKS, COMPANY } from '@constants';
|
|||
<span class="contact-label">Ежедневно 9:00–21:00</span>
|
||||
</div>
|
||||
|
||||
<!-- Кнопка входа ОСТАВЛЕНА ВНИЗУ -->
|
||||
<a href="/auth/sign-in" class="mobile-login-btn">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<!-- Кнопка входа/выхода -->
|
||||
<a href="/auth/sign-in" class="mobile-login-btn" id="mobile-auth-btn">
|
||||
<svg id="mobile-auth-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"></path>
|
||||
<polyline points="10 17 15 12 10 7"></polyline>
|
||||
<line x1="15" y1="12" x2="3" y2="12"></line>
|
||||
</svg>
|
||||
<span>Вход в аккаунт</span>
|
||||
<span id="mobile-auth-text">Вход в аккаунт</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -396,6 +396,8 @@ import { NAV_LINKS, COMPANY } from '@constants';
|
|||
font-size: 0.95rem;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 4px 15px rgba(234, 194, 110, 0.3);
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.mobile-login-btn:hover {
|
||||
|
|
@ -407,6 +409,39 @@ import { NAV_LINKS, COMPANY } from '@constants';
|
|||
transform: translateY(0);
|
||||
}
|
||||
|
||||
/* Кнопка выхода - красная как в десктопе */
|
||||
.mobile-login-btn.logout {
|
||||
background: #ef4444;
|
||||
color: #fff;
|
||||
box-shadow: 0 4px 15px rgba(239, 68, 68, 0.3);
|
||||
}
|
||||
|
||||
.mobile-login-btn.logout:hover {
|
||||
background: #dc2626;
|
||||
box-shadow: 0 6px 25px rgba(239, 68, 68, 0.4);
|
||||
}
|
||||
|
||||
/* Сообщение о входе - жёлтое */
|
||||
.mobile-login-btn.auth-message.login-message {
|
||||
background: #f59e0b;
|
||||
color: #1f2937;
|
||||
box-shadow: 0 4px 15px rgba(245, 158, 11, 0.4);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.mobile-login-btn.auth-message.login-message:hover {
|
||||
background: #d97706;
|
||||
box-shadow: 0 6px 25px rgba(245, 158, 11, 0.5);
|
||||
}
|
||||
|
||||
/* Сообщение о выходе - серое */
|
||||
.mobile-login-btn.auth-message.logout-message {
|
||||
background: #6b7280;
|
||||
color: #fff;
|
||||
box-shadow: 0 4px 15px rgba(107, 114, 128, 0.4);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.mobile-menu::-webkit-scrollbar {
|
||||
width: 4px;
|
||||
}
|
||||
|
|
@ -486,5 +521,102 @@ import { NAV_LINKS, COMPANY } from '@constants';
|
|||
let touchEndX = (e as TouchEvent).changedTouches[0].screenX;
|
||||
if (touchEndX - touchStartX < -50) closeMenu(); // Свайп влево
|
||||
});
|
||||
|
||||
// Проверка авторизации и настройка кнопки
|
||||
const authBtn = document.getElementById('mobile-auth-btn');
|
||||
const authText = document.getElementById('mobile-auth-text');
|
||||
const authIcon = document.getElementById('mobile-auth-icon');
|
||||
|
||||
const token = localStorage.getItem('auth_token');
|
||||
const userData = localStorage.getItem('user');
|
||||
|
||||
// Функция показа сообщения в блоке авторизации
|
||||
const showAuthMessage = (message: string, isLogin: boolean) => {
|
||||
if (!authText || !authBtn || !authIcon) return;
|
||||
|
||||
authText.textContent = message;
|
||||
authBtn.classList.remove('logout');
|
||||
authBtn.classList.add('auth-message');
|
||||
if (isLogin) {
|
||||
authBtn.classList.add('login-message');
|
||||
} else {
|
||||
authBtn.classList.add('logout-message');
|
||||
}
|
||||
authBtn.href = '#';
|
||||
authIcon.innerHTML = isLogin
|
||||
? '<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline>'
|
||||
: '<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line>';
|
||||
};
|
||||
|
||||
// Функция восстановления кнопки "Войти"
|
||||
const restoreLoginButton = () => {
|
||||
if (!authText || !authBtn || !authIcon) return;
|
||||
|
||||
authText.textContent = 'Вход в аккаунт';
|
||||
authBtn.classList.remove('auth-message', 'login-message', 'logout-message');
|
||||
authBtn.href = '/auth/sign-in';
|
||||
authBtn.onclick = null;
|
||||
authIcon.innerHTML = '<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"></path><polyline points="10 17 15 12 10 7"></polyline><line x1="15" y1="12" x2="3" y2="12"></line>';
|
||||
};
|
||||
|
||||
// Функция восстановления кнопки "Выйти"
|
||||
const restoreLogoutButton = () => {
|
||||
if (!authText || !authBtn || !authIcon) return;
|
||||
|
||||
authText.textContent = 'Выйти';
|
||||
authBtn.classList.remove('auth-message', 'login-message', 'logout-message');
|
||||
authBtn.classList.add('logout');
|
||||
authBtn.href = '#';
|
||||
authIcon.innerHTML = '<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line>';
|
||||
|
||||
// Добавляем обработчик выхода
|
||||
authBtn.onclick = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
// Показываем сообщение о выходе
|
||||
showAuthMessage('Вы вышли из системы', false);
|
||||
|
||||
// Удаляем данные
|
||||
try {
|
||||
await fetch('/api/auth/logout', { method: 'POST' });
|
||||
} catch (err) {}
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('user');
|
||||
localStorage.removeItem('pocketbase_auth');
|
||||
|
||||
// Через 3 сек возвращаем кнопку входа
|
||||
setTimeout(() => {
|
||||
restoreLoginButton();
|
||||
}, 3000);
|
||||
};
|
||||
};
|
||||
|
||||
// Проверка флага сообщения о входе
|
||||
const loginMessage = sessionStorage.getItem('showLoginMessage');
|
||||
const shouldOpenMenu = sessionStorage.getItem('openMobileMenuOnLoad') === 'true';
|
||||
|
||||
if (shouldOpenMenu) {
|
||||
const overlay = document.getElementById('mobile-menu-overlay');
|
||||
const body = document.body;
|
||||
if (overlay) overlay.classList.add('active');
|
||||
body?.classList.add('menu-open');
|
||||
sessionStorage.removeItem('openMobileMenuOnLoad');
|
||||
|
||||
// Если есть сообщение о входе - показываем его
|
||||
if (loginMessage) {
|
||||
showAuthMessage(`Добро пожаловать, ${loginMessage}!`, true);
|
||||
sessionStorage.removeItem('showLoginMessage');
|
||||
|
||||
// Через 3 сек меняем на кнопку выхода
|
||||
setTimeout(() => {
|
||||
restoreLogoutButton();
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
// Если авторизован и нет сообщения о входе - показываем кнопку выхода
|
||||
if (token && userData && !loginMessage) {
|
||||
restoreLogoutButton();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
@ -112,8 +112,10 @@ const firstLetter = userName ? userName.charAt(0).toUpperCase() : userEmail?.cha
|
|||
localStorage.removeItem('user');
|
||||
localStorage.removeItem('pocketbase_auth');
|
||||
|
||||
// Перенаправляем на главную
|
||||
window.location.href = '/';
|
||||
// Перенаправляем на текущую страницу с параметром loggedout
|
||||
const currentUrl = window.location.pathname + window.location.search + window.location.hash;
|
||||
const separator = currentUrl.includes('?') ? '&' : '?';
|
||||
window.location.href = currentUrl + separator + 'loggedout=true';
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue