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-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
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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-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 {
|
|
|
|
await axios.get('/getInfo');
|
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');
|
|
|
|
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
|