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

155 lines
4.4 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-05-17 09:24:12 +00:00
import SmsVerification from '../pages/SmsVerification.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-05-12 08:19:20 +00:00
import TeamsPage from '@/pages/dashboard/team.vue';
2025-05-22 23:12:10 +00:00
import DownloadPage from '@/pages/dashboard/Download.vue';
import spacesPage from '@/pages/dashboard/Spaces.vue';
2025-05-22 23:12:10 +00:00
import MeetingPage from '@/pages/dashboard/Meeting.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
2025-05-17 12:29:45 +00:00
},
2025-05-12 08:40:36 +00:00
{
path: '/',
name: 'LoginPage',
component: LoginPage
},
2025-05-04 14:01:31 +00:00
{
path: '/resetPassword',
name: 'ResetPassword',
component: ResetPassword
},
2025-05-17 09:24:12 +00:00
{
path: '/SmsVerification',
name: 'SmsVerification',
component: SmsVerification
},
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 , title : 'از این داشبورد، کار با XRoom را آغاز کنید.'}
2025-05-03 11:02:18 +00:00
},
{
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 , title : 'فایل ها'}
2025-05-03 11:02:18 +00:00
},
2025-05-20 19:40:22 +00:00
{
path: '/dashboard/download',
name: 'download',
component: DownloadPage,
meta: { requiresAuth: true , title : 'دانلود ها'}
2025-05-20 19:40:22 +00:00
},
{
path: '/dashboard/spaces',
name: 'spaces',
component: spacesPage,
meta: { requiresAuth: true , title : 'فضاها'}
},
2025-05-22 23:12:10 +00:00
{
path: '/dashboard/meetings',
name: 'meetings',
component: MeetingPage,
meta: { requiresAuth: true , title : 'جلسات'}
2025-05-22 23:12:10 +00:00
},
2025-05-12 08:19:20 +00:00
{
path: '/dashboard/teams',
name: 'teams',
component: TeamsPage,
meta: { requiresAuth: true , title : 'تیم ها'}
2025-05-12 08:19:20 +00:00
},
2025-05-03 11:02:18 +00:00
{
path: '/dashboard/edit-profile',
name: 'EditProfile',
component: () => import('@/pages/dashboard/EditProfile.vue'),
meta: { requiresAuth: true , title : 'ویرایش پروفایل'}
2025-05-10 10:27:19 +00:00
},
{
path: '/dashboard/readyPlayer',
name: 'ReadyPlayer',
component: () => import('@/pages/dashboard/readyPlayer.vue'),
meta: { requiresAuth: true , title : 'انتخاب آواتار'}
2025-05-05 11:28:49 +00:00
},
{
path: '/dashboard/ChangeAvatar',
name: 'ChangeAvatar',
component: () => import('@/pages/dashboard/ChangeAvatar.vue'),
meta: { requiresAuth: true , title : 'انتخاب آواتار'}
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-05-17 12:29:45 +00:00
return next('/');
2025-04-21 12:47:59 +00:00
}
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
2025-05-20 06:33:05 +00:00
let response = await axios.get('/getInfo');
response = response.data;
2025-05-03 13:39:59 +00:00
// 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-05-17 12:29:45 +00:00
if (to.path === '/') {
2025-04-21 12:47:59 +00:00
return next('/dashboard');
}
2025-05-03 11:02:18 +00:00
2025-05-05 14:35:42 +00:00
// Check if profile_glb is empty and not already going to ChangeAvatar
const customer = response.data.customer || JSON.parse(localStorage.getItem('customer') || '{}');
2025-05-17 09:24:12 +00:00
if (!customer.is_sms_verified && to.name !== 'SmsVerification') {
return next('/SmsVerification');
2025-05-05 14:35:42 +00:00
}
2025-05-17 09:24:12 +00:00
else if (!customer.profile_glb && to.name !== 'ReadyPlayer') {
return next('/dashboard/readyPlayer');
}
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-05-17 12:29:45 +00:00
return next('/');
2025-04-21 12:47:59 +00:00
}
}
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