LoginSignup
1
0

【Laravel6 / Vue2】authを使ってログアウト処理

Last updated at Posted at 2024-03-29

完全honaki用
悪しからず

コード

api.php
Auth::routes();
HeaderComponent.vue
<template>
    <div class="container-fluid bg-dark mb-3">
        <div class="container">
            <nav class="navbar navbar-dark">
                <span class="navbar-brand mb-0 h1">Budget Management</span>
                <div>
                    // ⭐️↓ここ
                    <button class="btn btn-danger" @click="logout">ログアウト</button>
                </div>
            </nav>
        </div>
    </div>
</template>
<script>
    export default {
        data: function () {
        },
        methods: {
            // ⭐️↓ここ
            logout(){
                axios.post('/api/logout')
                setTimeout(function () {
                    window.location.href = '/login'
                })
            },
        }
    }
</script>

補足

logout(){
    axios.post('/api/logout')
    //⭐️↓ここ
    setTimeout(function () {
        window.location.href = '/login'
    })
},

本来は⭐️の箇所を「this.$router.push」など別の方法で遷移したかったが、
loginに関して一切Vueを使っていないため
下記のログアウト処理後に

axios.post('/api/logout')

下記の処理で

setTimeout(function () {
    window.location.href = '/login'
})

loginページへ遷移するようにゴリ押ししました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0