index.js
1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from "../views/Login.vue";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView,
meta: {
title: 'SN管理',
requiresAuth: true // 添加这行,标记该路由需要认证
}
},
{
path: '/login',
name: 'Login',
component: Login,
meta: {
requiresAuth: false // 明确表示登录页不需要认证
}
}
]
})
// 路由守卫
router.beforeEach((to, from, next) => {
const isAuthenticated = sessionStorage.getItem('isAuthenticated')
if (to.path === '/login' && !isAuthenticated) {
return next('/login')
}
// 需要认证但未登录 → 跳转登录页
if (to.meta.requiresAuth && !isAuthenticated) {
return next('/login')
}
// 已登录但访问登录页 → 跳转首页
if (to.path === '/login' && isAuthenticated) {
return next('/')
}
next()
})
export default router