[Vuejs]-How to make work laravel, vue session csrf

1👍

If you are using axios with Vue2 for your ajax requests csrf is already available in bootstrap.js, you could see there this line

let token = document.head.querySelector('meta[name="csrf-token"]');

If you want to insist for your ajax only, then you would have put this line in your mounted hook.

mounted() {
    this.csrfToken = document.querySelector('meta[name="csrf-token"]').content
},

this csrfToken should be attaached in your forms like this one

<form :method="method.toUpperCase() == 'GET' ? 'GET' : 'POST'">
    <input-hidden :value="csrfToken" name="_token"/>

    <input-hidden
        v-if="['GET', 'POST'].indexOf(method.toUpperCase()) === -1"
        :value="method"
        name="_method"
    />

    <!--
        This hidden submit button accomplishes 2 things:
            1: Allows the user to hit "enter" while an input field is focused to submit the form.
            2: Allows a mobile user to hit "Go" in the on-screen keyboard to submit the form.
    -->
    <input type="submit" class="absolute invisible z-0">

    <slot/>
</form>

for good example of it. check this out csrf token

Note: Please don’t disable csrf token cause it’s only your way to secure your requests to the server.

Leave a comment