Writing an auth guard
An auth guard is a piece of middleware in our router. We can write functions and trigger them in routes. An excellent example of a function that we can put in a router is a function that helps with authentication. The function would then check whether the user of the application is authenticated or not. We must then show specific pages to users that are authenticated. So, let's start writing our auth guard.
Create a JavaScript file called auth.guard.js
in the auth
folder and add the following code:
import store from "@/store"; export const authGuard = (to, from, next) => { Â Â console.log("authGuard"); Â Â const authRequired = to.matched.some((record) => Â Â record.meta.requiresAuth); Â Â if (authRequired) { Â Â Â Â if (store.getters["authModule/isAuthenticated"]) { Â Â Â Â Â Â next(); Â Â Â Â Â Â return; Â Â ...