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

54 lines
1.3 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, from, next) => {
if (to.meta.requiresAuth) {
let isAuthenticated = false;
if (localStorage.getItem("user-token") != null){
isAuthenticated = true;
}
if (!isAuthenticated) {
// Redirect to the login page
next('/login');
} else {
// Proceed to the protected route
next();
}
} else {
// For routes that don't require authentication, proceed without checking
next();
}
});
export default router