3

I am trying to call a laravel route Route::get('/logout', 'Auth\LoginController@logout'); that will logout the user and redirect to the login page, but when I try to redirect to this url it won't work, anything happens, just like it was calling a vue router route, but this route does not exists in my router.js.

This is my route configuration to vue routes:

Route::get('/{vue_capture?}', function () {
    return view('index');
})->where('vue_capture', '^(?!storage).*$');

and this is my router.js :

//import ...

Vue.use(Router)

export default new Router({
    mode: 'history',
    routes: [{
        path: '/',
        component: Inicio
    }, {
        path: '/viagens/cadastrar',
        component: Cadastrar
    }, {
        path: '/viagens/listar',
        component: Listar
    }]
})

There's not a /logout route in my route.js, so why it is not calling my laravel route??

2
  • I don't understand ... Where are you calling the laravel logout route? Commented Nov 29, 2019 at 20:22
  • @porloscerrosΨ in a button link, but even directly in the URL it does not work Commented Nov 29, 2019 at 20:25

4 Answers 4

3

How are you generating your links inside your vue component: with router-link or href ?

  • If you want to call a vue route, use router-link;
  • If you want to call a "normal" or laravel route, use href;

Let me know if it worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to put your vue routes before this line:-

Auth::routes();

1 Comment

can you add to your answer why this would fix their issue? because this should be before the catchall route not after it
0

First, add Laravel auth routes to web.php file in routes folder

Auth::routes();

then in the frontend send a request to logout route

  axios.post('/logout').then(response => {
                location.reload();
            }).catch((error) => {
               console.log(error);
            });

4 Comments

I can do post requests like this, but I'm trying to do a get request to a laravel route through URL. What should I do in this case?
are you using Laravel Passport?
No, I'm using laravel's default Auth adapter.
try this in your route file Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
0

What I did in myproject.There is Auth::routes() in web.php. It will handle it by calling ougout in AthenticatesUser.php.Please have a look at this php class.

<template>
<div class="container">
    <button class="btn" @click="logout">logout</button>
    <form id="logout-form" action="/logout" method="POST" style="display: 
none;">
                        <input type="hidden" name="_token" :value="token">
                    </form>
    <router-view></router-view>
</div>
</template>
 <script>
  export default {
  computed: {
        token() {
            let token = document.head.querySelector('meta[name="csrf-token"]');
            return token.content
        }
    },
    methods: {
        logout() {
            document.getElementById('logout-form').submit()
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.