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; ...