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
This commit is contained in:
@@ -31,6 +31,9 @@
|
|||||||
<router-link to="/" class="dropdown-nav-link" @click="closeMobileMenu">
|
<router-link to="/" class="dropdown-nav-link" @click="closeMobileMenu">
|
||||||
🏠 Главная
|
🏠 Главная
|
||||||
</router-link>
|
</router-link>
|
||||||
|
<router-link to="/profile" class="dropdown-nav-link" @click="closeMobileMenu">
|
||||||
|
👤 Профиль
|
||||||
|
</router-link>
|
||||||
<router-link to="/about" class="dropdown-nav-link" @click="closeMobileMenu">
|
<router-link to="/about" class="dropdown-nav-link" @click="closeMobileMenu">
|
||||||
👥 О нас
|
👥 О нас
|
||||||
</router-link>
|
</router-link>
|
||||||
|
|||||||
@@ -44,8 +44,11 @@ app.use(router)
|
|||||||
// Инициализация auth store после создания app
|
// Инициализация auth store после создания app
|
||||||
import { useAuthStore } from './stores/auth'
|
import { useAuthStore } from './stores/auth'
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
authStore.initializeAuth()
|
// Инициализируем авторизацию
|
||||||
|
authStore.initializeAuth().then(() => {
|
||||||
|
console.log('Auth initialization completed')
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Auth initialization failed:', error)
|
||||||
|
})
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import Home from '../views/Home.vue'
|
import Home from '../views/Home.vue'
|
||||||
|
import { useAuthStore } from '../stores/auth'
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
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
|
export default router
|
||||||
|
|||||||
@@ -133,8 +133,15 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
if (token.value) {
|
if (token.value) {
|
||||||
// Восстанавливаем заголовок авторизации
|
// Восстанавливаем заголовок авторизации
|
||||||
axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}`
|
axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}`
|
||||||
// Загружаем данные пользователя
|
try {
|
||||||
await fetchProfile()
|
// Загружаем данные пользователя
|
||||||
|
await fetchProfile()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Ошибка инициализации авторизации:', error)
|
||||||
|
// Если токен невалидный, очищаем его
|
||||||
|
clearToken()
|
||||||
|
clearUser()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +163,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
fetchProfile,
|
fetchProfile,
|
||||||
initializeAuth,
|
initializeAuth,
|
||||||
setToken,
|
setToken,
|
||||||
clearToken
|
clearToken,
|
||||||
|
clearUser
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
[file name]: Profile.vue
|
|
||||||
[file content begin]
|
|
||||||
<template>
|
<template>
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<h1>👤 Личный кабинет</h1>
|
<h1>👤 Личный кабинет</h1>
|
||||||
@@ -384,5 +382,4 @@ export default {
|
|||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
[file content end]
|
|
||||||
Reference in New Issue
Block a user