API Authentication Using Laravel 9 Sanctum and Vue js 3 Part 2

1 year ago admin Laravel

In the second part of this tutorial, we are going to add routes to our API Authentication system and we will add also the main component.


Create the vue routes

Inside the router folder, we add a new file index.js here we define all of our routes.

                                                    
                                                                                                                
import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/components/Home.vue";
import Login from "@/components/Login.vue";
import { useAuthStore } from '@/stores/useAuthStore.js';


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

function checkIfNotLogged() {
    const store = useAuthStore();
    if (store.getUser) return '/';
}


const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            name: "home",
            component: Home,
            beforeEnter: [checkIfLogged],
        },
        {
            path: "/login",
            name: "login",
            component: Login,
            beforeEnter: [checkIfNotLogged],
        },
    ],
});

export default router;

Create the Header Component

Inside the components folder, we add a new file Header.vue.

                                                        
                                                                                                                        
<template>
    <nav class="navbar navbar-expand-lg navbar-light bg-white rounded-0 shadow-sm">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Laravel Inventory</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mb-2 mb-lg-0">
                    <li class="nav-item">
                        <router-link class="nav-link active" aria-current="page" to="/">Home</router-link>
                    </li>
                </ul>
                <ul v-if="!store.getUser" class="navbar-nav mb-2 mb-lg-0">
                    <li class="nav-item">
                        <router-link class="nav-link" aria-current="page" to="/login">Login</router-link>
                    </li>
                </ul>
                <ul v-else class="navbar-nav mb-2 mb-lg-0">
                    <li class="nav-item">
                        <router-link class="nav-link" aria-current="page" to="#">
                            {{ store.getUser.data.name }}
                        </router-link>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link" @click="userLogout" style="cursor:pointer">Logout</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</template>

<script setup>
    import { useAuthStore } from '@/stores/useAuthStore.js';
    import Swal from 'sweetalert2';
    import router from '@/router';

    const store = useAuthStore();

    const userLogout = async () => {
        
        try {
            const response = await axios.get('/api/logout', store.getHeaderConfig);
            Swal.fire({
                icon: 'success',
                text: response.data.message,
            });
            store.clearStoredData();
            router.push('/login');
        } catch (error) {
            console.log(error);
        }
    }
</script>

<style>
</style>

Create the Main Component

Inside the components folder, we add a new file Main.vue.

                                                        
                                                                                                                        
<template>
    <Header></Header>
    <router-view></router-view>
</template>

<script setup>
    import Header from './Header.vue';
    import { onMounted } from "vue";
    import { useAuthStore } from '@/stores/useAuthStore.js';

    const store = useAuthStore();

    onMounted(() => {
        store.setUser();
    });
</script>

<style>
</style>

Create the Home Component

Inside the components folder, we add a new file Home.vue.

                                                        
                                                                                                                        
<template>
   <div class="container">
      <div class="row my-5">
         <div class="col-md-4">
            <div class="card">
               <div class="card-header">
                  sidebar
               </div>
               <div class="card-body">
                  
               </div>
            </div>
         </div>
         <div class="col-md-8">
            <div class="card">
               <div class="card-header">
                  Logged in!
               </div>
               <div class="card-body">
                  
               </div>
            </div>
         </div>
      </div>
   </div>
</template>

<script setup>
</script>

<style>
</style>

Create the Login component

Inside the components folder, we add a new file Login.vue.

                                                        
                                                                                                                        
<template>
  <div class="container">
    <div class="row my-5">
      <div class="col-md-6 mx-auto">
        <ul class="list-group my-2" v-for="(errorArray, index) in store.getErrors" :key="index">
            <li class="list-group-item bg-danger text-white mb-1" v-for="(error, index) in errorArray" :key="index">
                {{error}}
            </li>
        </ul>
        <div class="card">
          <div class="card-header bg-white">
            <h4 class="text-center">
              Login
            </h4>
          </div>
          <div class="card-body">
            <div class="form-group mb-3">
              <input 
                type="text" 
                v-model="data.user.email"
                placeholder="Email*" class="form-control">
            </div>
            <div class="form-group mb-3">
              <input 
                type="password" 
                v-model="data.user.password"
                placeholder="Password*" class="form-control">
            </div>
            <div class="form-group mb-3">
              <button @click="userAuth" class="btn btn-primary btn-sm">
                Login
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { reactive } from "vue";
  import Swal from 'sweetalert2';
  import router from '@/router';
  import { useAuthStore } from '@/stores/useAuthStore.js';
  

  const store = useAuthStore();
  
  const data = reactive({
    user: {
      email: '',
      password: ''
    }
  });

  const userAuth = async () => {
    store.clearErrors();
    try {
      const response = await axios.post('/api/login', data.user);
      if(response.data.success){
        store.storeUser(response.data.user);
        router.push('/');
      }else{
        Swal.fire({
          icon: 'error',
          title: 'Oops...',
          text: response.data.message,
        });
      }
    } catch (error) {
      store.setErrors(error.response.data.errors);
    }
  }
</script>

<style>
</style>

Related Tuorials

How to Conditionally Include a Blade Template in Laravel

In this lesson, we will see how to conditionally include a blade template in Laravel.Sometimes,...


How to Include a Blade Template Only if it Exists in Laravel

In this lesson, we will see how to include a blade template only if it exists in Laravel.Sometimes,&...


How to Pass a Variable to Include in Laravel

In this lesson, we will see how to pass a variable to include in Laravel. Sometimes, we want to pass...


How to the Get the Previous and Next Posts in Laravel

In this lesson, we will see how to get the previous and next posts in Laravel, sometimes when you ge...


How Do you Format a Number as K/M/B in Laravel

In this lesson, we will see how to format a number as K/M/B in Laravel, sometimes we want to display...


How to Override Laravel Fortify Default Registration Redirect

In this lesson, we will see how to override Laravel Fortify default registration redirect, sometimes...


How to Override Laravel Fortify Default Login Redirect

In this lesson, we will see how to override Laravel Fortify default login redirect, sometimes when w...


How to Use the Same Validation Form Request for both Create and Update in Laravel

In this lesson, we will see how to use the same validation form request for both create and update i...


How to Get Raw SQL Output from the Eloquent Model in Laravel 11

In this lesson, we will see how to get raw SQL output from the eloquent model in Laravel 11, sometim...


How to Check if a Record Does Not Exist in Laravel

in this lesson, we will see how to check if a record does not exist in laravel, sometimes you need t...