Issue #140: Done.

This commit is contained in:
chiayin
2023-10-13 17:58:47 +08:00
parent 1cfbddf510
commit cf3d799ed7
4 changed files with 191 additions and 39 deletions

View File

@@ -11,7 +11,7 @@ import NotFound404 from '@/views/NotFound404.vue';
const routes = [
{
path: '/', // 預設進入路由
redirect: '/files' //重定向
redirect: '/files', //重定向
},
{
path: '/',
@@ -29,6 +29,10 @@ const routes = [
path: "/",
name: "MainContainer",
component: MainContainer,
meta: {
title: "MainContainer",
requiresAuth: true
},
children: [
{
path: "/member-area",
@@ -65,13 +69,10 @@ const routes = [
},
];
const base_url = import.meta.env.BASE_URL;
const router = createRouter({
history: createWebHistory(base_url), //(/)
// history: createWebHashHistory(base_url), // (/#)
routes
});
@@ -79,15 +80,13 @@ const router = createRouter({
router.beforeEach((to, from) => {
// to: Route: 即將要進入的目標 路由物件
// from: Route: 當前導航正要離開的路由
const nextRoute = ['MemberArea', 'Files'];
let isCookie = document.cookie.split(';').some(c => c.trim().startsWith('luciaToken=')); // 是否登入
// 未登入狀態當路由到nextRoute指定頁時跳轉至login
if (nextRoute.indexOf(to.name) >= 0) {
if (!isCookie) router.push({ name: 'Login' });
// 需要驗證登入的頁面,判斷有無登入,無登入要轉址 login
if(to.meta.requiresAuth) {
if (!isCookie) router.push({ name: 'Login' });
}
// 已登入狀態;當路由到login時跳轉至home
// 當路由到 login 時,有登入要跳轉至home
if (to.name === 'Login') {
if (isCookie) router.push({ name: 'Files' });
}