Quite often in your application, you could come across when certain pages/routes could be accessed by only authenticated users. And then there are other pages, which could be access be any one.
I tend to call them by private routes and public routes. Private Routes requires a certain condition to be met, for example, the user needs to be authenticated. We could also have additional conditions like the user needs to have a certain privilege. If the condition is not met, the user attempting to navigate to the private route needs to be redirected to a common route, usually in public route.
In VueJs, the Navigation guards allows you to intercept the navigations and conditionally evaluate whether or not to accept the redirection or cancel it. While there are multiple ways to intercept the navigation process, per-route, in-component, the easiest way would be to use the the Global Guards.
Global Before Guards
The beforeEach guard method is probably the most common approach to handle navigations. The method is called each time when a new navigation starts.
You could add the validation code in the beforeEach method to ensure only the validated users access the pages.
const router = new VueRouter({
routes,
});
router.beforeEach((to, from, next) => {
// Your validation logic goes here.
});
The beforeEach Method accepts 3 parameters.
- to : The future Route
- from : the current Route
- next : a function that should be called to resolve the route successfully.
Real World Scenario
Let us consider a real world scenario. You have couple of routes in your application.
const routes = [
{
path: "/",
name: "Default",
component: Default
},
{
path: "/login",
name: "Login",
component: Login
},
{
path: "/dashboard",
name: "Dashboard",
component: Dashboard
},
];
Ideally, you would like the Default and Login to be accessed by anyone. However, the Dashboard should be accessed only by authenticated users only.
We begin by including this very bit of logic in our route definition. We could use the meta tags for including additional information. Redefining our routes now,
const routes = [
{
path: "/",
name: "Default",
component: Default,
meta: {
requiresAuth: false,
},
},
{
path: "/login",
name: "Login",
component: Login,
meta: {
requiresAuth: false,
},
},
{
path: "/dashboard",
name: "Dashboard",
component: Dashboard,
meta: {
requiresAuth: true,
},
},
];
As you can observe, we have added a requiresAuth flag within the meta information of routes. With this in place, we could now alter our beforeEach code as following.
router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (isAuthenticated()) {
next();
} else {
next("/");
}
} else {
next();
}
});
The method now checks if the to route has the meta information flag of requiresAuth set to true and if so, calls your custom isAuthenticated method to verify if the user is authenticated to access the page.
If the user is authenticated to access the page, the next() method is called to complete the navigation successfully. Otherwise, you override the to route and navigate away to a custom route, in this case using next(“/”).
That’s all your require for creating private routes using VueJs. If you would like to see the complete code, you could find the same here in my Github.