Skip to content

Commit

Permalink
- fix redirect between /profile and /auth
Browse files Browse the repository at this point in the history
  • Loading branch information
fizfakovets committed Oct 31, 2023
1 parent 6b9c0ec commit 71a429a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/router/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RouteRecordRaw } from 'vue-router';
import { LocalStorage, LocalStorageItem } from '@/models';
import { NavigationGuard, RouteRecordRaw } from 'vue-router';

export const authRoutes: RouteRecordRaw[] = [
{
Expand Down Expand Up @@ -26,3 +27,11 @@ export const authRoutes: RouteRecordRaw[] = [
component: () => import('@/views/auth/OauthLoginView.vue'),
},
];

export const authHandler: NavigationGuard = to => {
if (to.path.startsWith('/profile') && !LocalStorage.get(LocalStorageItem.Token)) {
return { path: '/auth' };
} else if (to.path.startsWith('/auth') && LocalStorage.get(LocalStorageItem.Token)) {
return { path: '/profile' };
}
};
11 changes: 2 additions & 9 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useProfileStore } from '@/store';
import { marketingApi } from '@/api/marketing/MarketingApi';
import { LocalStorage, LocalStorageItem } from '@/models/LocalStorage';
import { adminRoutes, adminHandler } from './admin';
import { profileRoutes } from './profile';
import { authRoutes } from './auth';
import { authHandler, authRoutes } from './auth';
import { AppsView } from '@/views';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { timetableRoutes, timetableHandler } from './timetable';
Expand Down Expand Up @@ -52,13 +51,7 @@ const router = createRouter({
routes,
});

router.beforeEach(async to => {
if (to.path === '/profile' && !LocalStorage.get(LocalStorageItem.Token)) {
return { path: '/auth' };
} else if (to.path === '/auth' && LocalStorage.get(LocalStorageItem.Token)) {
return { path: '/profile' };
}
});
router.beforeEach(authHandler);
router.beforeEach(timetableHandler);
router.beforeEach(adminHandler);

Expand Down

0 comments on commit 71a429a

Please sign in to comment.