1

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;
2
  • I think your guard is always redirecting to select-corporation even if target route is select-corporation itself. So it's infinite redirect loop. Commented Nov 25, 2018 at 14:45
  • @MaxSinev. yeah i believe thats the problem. So how i can fix this i already put the else condition? thanks Commented Nov 25, 2018 at 14:47

2 Answers 2

1

Check that your target route is not select-corporation to interrupt infinite redirecting loop, also "else" block causes the same behavior if corporation is selected already:

    router.beforeEach((to, from, next) => {
        if (localStorage.getItem('selectedCorporation') === null) {
            // checking to avoid loop
            if (to.name === 'corporations.select') return next();
            next({
                path: '/select-corporation'
            });
        }
        else {
           next();
        }
     });

If you want to redirect user to branches page after corp selected, just do it in CorporationsSelect instead.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer. But still the same error. Maximum call stack exceeded
@Joseph you missed / in the beginning of corporation route path, add it and try again.
I updated my answer code a little bit. You can check this jsfiddle i created to test your issue jsfiddle.net/3px84r2b there is no error...
I got it. Thanks a lot. Have a great day
When i try to click other button when selectedCorporation is null, thats when the maximum call stack appears. Any idea how to fix this?
|
0

you may add a check. if coming from and next path is not same then go for path.

 if (localStorage.getItem('selectedCorporation') === null) {
    if (to.redirectedFrom !== to.path) {
       next({path: '/select-corporation'});
      } 
     } else {
        next({path: '/branches' });
     }

it worked for me .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.