Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global before hook except for 1 page #163

Closed
ultimate-tester opened this issue Oct 20, 2017 · 1 comment
Closed

Global before hook except for 1 page #163

ultimate-tester opened this issue Oct 20, 2017 · 1 comment

Comments

@ultimate-tester
Copy link

I would like to use the global before hook to check if I have a valid login session:

router.hooks({
        before: function (done, params) {
            if (global.sessionId === -1) {
                router.navigate('/login/');
                return;
            }

            done();
        }
    });

This creates an infinite loop, because even on the login page the sessionId is -1. Since I want my full website to be protected by this sessionId check, I would like to know how I can have this global hook for every route except for the /login/ route. Thanks in advance!

krasimir pushed a commit that referenced this issue Dec 27, 2020
@krasimir
Copy link
Owner

With the latest 8.0.0 release you'll be able to do the following:

router.hooks({
    before: function (done, match) {
      if (window.sessionId === -1 && match.url !== "login") {
        console.log("redirect to login");
        router.navigate("/login");
        return;
      }
      done();
    },
  });

The second argument of the hook is a Match object. You can use its url field to check what's the path that matched.

P.S.
as of today version 8.0.0 is not released officially. You could have the beta one by running yarn add navigo@beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants