[Vuejs]-How can I redirect back to previous page after login on vue and laravel?

-1👍

Laravel requires you do do a POST request when logging out.

In order to do this you will need the csrf token and the logout url.

I would pass these 2 as props (in a blade template) :

<my-component 
    logout-url="{{route('logout')}}" 
    csrf-token="{{ csrf_token() }}">
</my-component>

Then you should add a hidden form to your template and add the appropriate logic:

<template>
    <a v-if="auth" href="javascript:" class="btn btn-default btn-block" @click="add($event)">
        Add
    </a>
    <a v-else href="javascript:" class="btn btn-default btn-block" @click="logout">
        Add
    </a>
    <form id="vue-logout-form" v-action="{{ logoutUrl }}" method="POST" style="display: none;">
        <input name="_token" type="hidden" value="{{ csrfToken }}">
    </form>
</template>
<script>
    export default {
        props: {
            logoutUrl:{type: String},
            csrfToken:{type: String}
        },
        [...]
        methods: {
            logout() {
                document.getElementById("vue-logout-form").submit();
            }
        }
    }
</script>
 

Basically you will make the post request by calling the logout() method

👤ka_lin

Leave a comment