How to Add an Auth Middleware with Vue-router

2 years ago admin Vuejs

In this lesson we are going to see how to make an auth middleware using vue-router, the middleware will check if the user is logged in before accessing the route.


Create the route guard

Let's assume that we have a profile page and the user must be logged in to access this page.

We can use a route guard that checks if the user is logged in.

In the code below inside the router, we import a store with Pinia that returns a login status equal to true or false, and we check if the user is logged in he is ok to go if not he is redirected to the login page.

                                                    
                                                                                                                
import {createRouter, createWebHistory}  from 'vue-router';
import Profile from '@/components/Profile.vue';
import { useAuthStore } from '@/store/useAuthStore';


function checkIfLogged() {
    const authStore = useAuthStore();
    if (!authStore.getLoggedInstatus) return '/login';
}

const routes = [
    {
        name: 'profile',
        path: '/user/profile',
        component: Profile,
        beforeEnter: [checkIfLogged], 
    }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

Related Tuorials

How to Persist Data in the Pinia Store

In this lesson, we will see how to persist data in the Pinia store.When working with Pinia...


How to Reset the File input Field in Vue js

In this lesson, we will see how to reset the file input field in Vue js.When uploading files using V...


Review App Using Laravel 11 & Vue js 3 Composition API Part 5

In the last part of this tutorial, we will display the reviews list of each product, add the ability...


Review App Using Laravel 11 & Vue js 3 Composition API Part 4

In the fourth part of this tutorial, we will fetch and display all the products on the home page, vi...


Review App Using Laravel 11 & Vue js 3 Composition API Part 3

In the third part of this tutorial, we will start coding the front end, first, we will install the p...


Review App Using Laravel 11 & Vue js 3 Composition API Part 2

In the second part of this tutorial, we will create the product and review controllers, and later we...


Review App Using Laravel 11 & Vue js 3 Composition API Part 1

In this tutorial, we will create a review app using Laravel 11 & Vue js 3 Composition API, the user...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 5

In the final part of this tutorial, we will display the cart items, add the ability to increment/dec...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 4

In the fourth part of this tutorial, we will fetch and display all the products, and add the store w...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 3

In the third part of this tutorial, we will move to the front end, we will install the packages...