I have common problem like other people here and i try to follow their solution but i still can't get it through. Here in my app, i need to login first then select the corporation before proceeding to the main page. I have no problem with the login, my problem is on the select corporation. I need to attach a guard that could prevent them from accessing the main page if they have not chosen a corporation. What i did is check the localstorage if the selectCorporation is empty, then i will be able to know that they have selected a corporation.
const router = new Router({
mode: 'history',
routes: [
{ path: '/', name: 'overview', component: Overview },
// Authentication
{ path: '/auth/login', name: 'auth.login', component: Login, meta: { requiresVisitor: true }},
//Select Corporation
{ path: 'select-corporation', name: 'corporations.select', component: CorporationsSelect }
// Branches
{ path: '/branches', name: 'branches.index', component: BranchesIndex },
{ path: '/branches/create', name: 'branches.create', component: BranchesCreate },
{ path: '/branches/:id', name: 'branches.view', component: BranchesView },
{ path: '/branches/:id/edit', name: 'branches.edit', component: BranchesEdit },
});
router.beforeEach((to, from, next) => {
if (localStorage.getItem('selectedCorporation') === null) {
next({
path: '/select-corporation'
});
} else {
next({
path: '/branches'
});
}
});
export default router;
select-corporationeven if target route isselect-corporationitself. So it's infinite redirect loop.