Files
rss-reader/vue/src/router/index.js
T

44 lines
1.2 KiB
JavaScript

import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
scrollBehavior: () => ({ top: 0, behavior: 'instant' }),
routes: [
{
path: '/',
redirect: '/feeds',
},
{
path: '/feeds',
name: 'feeds',
// route level code-splitting
// this generates a separate chunk (Feed.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import('../views/FeedView.vue'),
meta: { requiresAuth: true },
},
{
path: '/admin',
name: 'admin',
component: () => import('../views/AdminView.vue'),
meta: { requiresAuth: true },
},
{
path: '/login',
name: 'login',
component: () => import('../views/LoginView.vue')
},
]
})
router.beforeEach((to) => {
const isAuthenticated = localStorage.getItem("user-token") != null;
// Redirect unauthenticated users hitting a protected route to login;
// returning a value (instead of the deprecated next() callback) is the
// modern vue-router guard API. Returning nothing lets navigation proceed.
if (to.meta.requiresAuth && !isAuthenticated) {
return '/login';
}
});
export default router