...
This commit is contained in:
@@ -1,5 +1,29 @@
|
||||
import AppLayout from '@/layout/AppLayout.vue';
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import axios from 'axios';
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
const loginGuard = async (to, from, next) => {
|
||||
const token = Cookies.get('bm-token');
|
||||
|
||||
if (!token) {
|
||||
next('/auth/login'); // Redirect to log in if no token
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get('/api/users/user/get-self', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
withCredentials: true
|
||||
});
|
||||
next();
|
||||
} catch (error) {
|
||||
console.log('Authentication error:', error);
|
||||
next('/auth/login');
|
||||
}
|
||||
};
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
@@ -7,12 +31,33 @@ const router = createRouter({
|
||||
{
|
||||
path: '/',
|
||||
component: AppLayout,
|
||||
beforeEnter: loginGuard,
|
||||
children: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'dashboard',
|
||||
component: () => import('@/views/Dashboard.vue')
|
||||
},
|
||||
{
|
||||
path: '/belonging/create-belonging',
|
||||
name: 'create-belonging',
|
||||
component: () => import('@/views/pages/Belonging/CreateBelonging.vue')
|
||||
},
|
||||
{
|
||||
path: '/belonging/list-belongings',
|
||||
name: 'list-belongings',
|
||||
component: () => import('@/views/pages/Belonging/ListBelongings.vue')
|
||||
},
|
||||
{
|
||||
path: '/categories/list-categories',
|
||||
name: 'list-categories',
|
||||
component: () => import('@/views/pages/Category/ListCategories.vue')
|
||||
},
|
||||
{
|
||||
path: '/features/list-features',
|
||||
name: 'list-features',
|
||||
component: () => import('@/views/pages/Features/ListFeatures.vue')
|
||||
},
|
||||
{
|
||||
path: '/uikit/formlayout',
|
||||
name: 'formlayout',
|
||||
|
||||
Reference in New Issue
Block a user