From 20d4913f50c9d9c93fa49d092f057309517f2756 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Sat, 11 Oct 2025 04:15:23 +0500 Subject: [PATCH] modified: begushiybashkir/bbvue/src/components/NavigationMenu.vue modified: begushiybashkir/bbvue/src/main.js modified: begushiybashkir/bbvue/src/router/index.js modified: begushiybashkir/bbvue/src/stores/auth.js modified: begushiybashkir/bbvue/src/views/Profile.vue add profile link into header menu fix bag with save state --- .../bbvue/src/components/NavigationMenu.vue | 3 +++ begushiybashkir/bbvue/src/main.js | 9 ++++--- begushiybashkir/bbvue/src/router/index.js | 24 +++++++++++++++++++ begushiybashkir/bbvue/src/stores/auth.js | 14 ++++++++--- begushiybashkir/bbvue/src/views/Profile.vue | 5 +--- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/begushiybashkir/bbvue/src/components/NavigationMenu.vue b/begushiybashkir/bbvue/src/components/NavigationMenu.vue index 08dc294..477120c 100644 --- a/begushiybashkir/bbvue/src/components/NavigationMenu.vue +++ b/begushiybashkir/bbvue/src/components/NavigationMenu.vue @@ -31,6 +31,9 @@ 🏠 Главная + + 👤 Профиль + 👥 О нас diff --git a/begushiybashkir/bbvue/src/main.js b/begushiybashkir/bbvue/src/main.js index b997f6c..76181bf 100644 --- a/begushiybashkir/bbvue/src/main.js +++ b/begushiybashkir/bbvue/src/main.js @@ -44,8 +44,11 @@ app.use(router) // Инициализация auth store после создания app import { useAuthStore } from './stores/auth' const authStore = useAuthStore() -authStore.initializeAuth() - - +// Инициализируем авторизацию +authStore.initializeAuth().then(() => { + console.log('Auth initialization completed') +}).catch(error => { + console.error('Auth initialization failed:', error) +}) app.mount('#app') diff --git a/begushiybashkir/bbvue/src/router/index.js b/begushiybashkir/bbvue/src/router/index.js index 9e0d385..ec39c9e 100644 --- a/begushiybashkir/bbvue/src/router/index.js +++ b/begushiybashkir/bbvue/src/router/index.js @@ -1,5 +1,6 @@ import { createRouter, createWebHistory } from 'vue-router' import Home from '../views/Home.vue' +import { useAuthStore } from '../stores/auth' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -79,6 +80,29 @@ const router = createRouter({ ] }) +router.beforeEach(async (to, from, next) => { + const authStore = useAuthStore() + + // Если пользователь переходит на защищенные страницы и не авторизован + if (to.meta.requiresAuth && !authStore.isAuthenticated) { + // Проверяем, есть ли токен в localStorage + if (authStore.token) { + try { + // Пытаемся загрузить профиль + await authStore.fetchProfile() + next() + } catch (error) { + console.log(error) + next('/login') + } + } else { + next('/login') + } + } else { + next() + } +}) + export default router diff --git a/begushiybashkir/bbvue/src/stores/auth.js b/begushiybashkir/bbvue/src/stores/auth.js index 7f4eeb6..20abafd 100644 --- a/begushiybashkir/bbvue/src/stores/auth.js +++ b/begushiybashkir/bbvue/src/stores/auth.js @@ -133,8 +133,15 @@ export const useAuthStore = defineStore('auth', () => { if (token.value) { // Восстанавливаем заголовок авторизации axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}` - // Загружаем данные пользователя - await fetchProfile() + try { + // Загружаем данные пользователя + await fetchProfile() + } catch (error) { + console.error('Ошибка инициализации авторизации:', error) + // Если токен невалидный, очищаем его + clearToken() + clearUser() + } } } @@ -156,6 +163,7 @@ export const useAuthStore = defineStore('auth', () => { fetchProfile, initializeAuth, setToken, - clearToken + clearToken, + clearUser } }) \ No newline at end of file diff --git a/begushiybashkir/bbvue/src/views/Profile.vue b/begushiybashkir/bbvue/src/views/Profile.vue index 4521e22..5eca4e2 100644 --- a/begushiybashkir/bbvue/src/views/Profile.vue +++ b/begushiybashkir/bbvue/src/views/Profile.vue @@ -1,5 +1,3 @@ -[file name]: Profile.vue -[file content begin]