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>
|
||||
<router-link to="/profile" class="dropdown-nav-link" @click="closeMobileMenu">
|
||||
👤 Профиль
|
||||
</router-link>
|
||||
<router-link to="/about" class="dropdown-nav-link" @click="closeMobileMenu">
|
||||
👥 О нас
|
||||
</router-link>
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
})
|
||||
@@ -1,5 +1,3 @@
|
||||
[file name]: Profile.vue
|
||||
[file content begin]
|
||||
<template>
|
||||
<div class="page">
|
||||
<h1>👤 Личный кабинет</h1>
|
||||
@@ -385,4 +383,3 @@ export default {
|
||||
}
|
||||
}
|
||||
</style>
|
||||
[file content end]
|
||||
Reference in New Issue
Block a user