XRoomDashboardFront/xroom-dashboard/src/router/index.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-04-14 13:09:00 +00:00
import { createRouter, createWebHistory } from 'vue-router'
2025-05-03 11:02:18 +00:00
import SignupPage from '../pages/SignupPage.vue'
import LoginPage from '../pages/LoginPage.vue'
2025-05-04 14:01:31 +00:00
import ResetPassword from '../pages/ResetPassword.vue'
2025-04-21 12:47:59 +00:00
import DashboardPage from '../pages/dashboard/index.vue'
2025-05-03 11:02:18 +00:00
import FilesPage from '@/pages/dashboard/files.vue';
2025-04-21 12:47:59 +00:00
import axios from '@/axios';
2025-04-14 13:09:00 +00:00
const routes = [
{
path: '/signup',
2025-05-03 11:02:18 +00:00
name: 'SignupPage',
2025-04-14 13:09:00 +00:00
component: SignupPage
},
{
path: '/login',
2025-05-03 11:02:18 +00:00
name: 'LoginPage',
2025-04-14 13:09:00 +00:00
component: LoginPage
},
2025-05-04 14:01:31 +00:00
{
path: '/resetPassword',
name: 'ResetPassword',
component: ResetPassword
},
2025-04-14 13:09:00 +00:00
{
path: '/dashboard',
2025-05-03 11:02:18 +00:00
name: 'DashboardPage',
component: DashboardPage,
meta: { requiresAuth: true }
},
{
2025-04-21 12:47:59 +00:00
path: '/dashboard/files',
2025-04-14 13:09:00 +00:00
name: 'files',
2025-05-03 11:02:18 +00:00
component: FilesPage,
meta: { requiresAuth: true }
},
{
path: '/dashboard/edit-profile',
name: 'EditProfile',
component: () => import('@/pages/dashboard/EditProfile.vue'),
meta: { requiresAuth: true }
2025-05-05 11:28:49 +00:00
},
{
path: '/dashboard/ChangeAvatar',
name: 'ChangeAvatar',
component: () => import('@/pages/dashboard/ChangeAvatar.vue'),
meta: { requiresAuth: true }
2025-04-14 13:09:00 +00:00
}
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
2025-04-21 12:47:59 +00:00
router.beforeEach(async (to, from, next) => {
const token = localStorage.getItem('token');
2025-05-03 11:02:18 +00:00
// Check if the route requires authentication
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
// If route doesn't require auth, continue
if (!requiresAuth) {
return next();
}
// If route requires auth but no token, redirect to login
if (requiresAuth && !token) {
2025-04-21 12:47:59 +00:00
return next('/login');
}
2025-05-03 11:02:18 +00:00
// If we have a token and it's an auth route, verify it
2025-04-21 12:47:59 +00:00
if (token) {
try {
2025-05-03 13:39:59 +00:00
// Make getInfo request
const response = await axios.get('/getInfo');
// Save customer and user data to localStorage
if (response.data?.customer) {
localStorage.setItem('customer', JSON.stringify(response.data.customer));
}
if (response.data?.user) {
localStorage.setItem('user', JSON.stringify(response.data.user));
}
localStorage.setItem('baseUrl','http://194.62.43.230:8000');
2025-05-03 11:02:18 +00:00
// If trying to access login page while authenticated, redirect to dashboard
2025-04-21 12:47:59 +00:00
if (to.path === '/login') {
return next('/dashboard');
}
2025-05-03 11:02:18 +00:00
2025-04-21 12:47:59 +00:00
return next();
} catch (err) {
2025-05-03 11:02:18 +00:00
// Invalid token, clear storage and redirect to login
2025-04-21 12:47:59 +00:00
localStorage.removeItem('token');
localStorage.removeItem('user');
2025-05-03 13:39:59 +00:00
localStorage.removeItem('customer');
2025-04-21 12:47:59 +00:00
return next('/login');
}
}
2025-05-03 11:02:18 +00:00
2025-04-21 12:47:59 +00:00
next();
});
2025-05-03 11:02:18 +00:00
export default router