How to Auth::check() user on Laravel with API calls

前端 未结 4 762
[愿得一人]
[愿得一人] 2021-01-21 07:54

I\'m using VueJS in my front-end, so I have this API call somewhere in the front-end code:

let products = axios.get(\'api/products\');


4条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 08:20

    Make sure you're sending the CSRF token, somewhere in your bootstrap.js file, you should have something like this. Also make sure you have the csrf token somewhere in the meta tag

    let token = document.head.querySelector('meta[name="csrf-token"]');
    
        if (token) {
            window.axios.defaults.headers.common = {
                'X-CSRF-TOKEN': Laravel.csrfToken,
                'X-Requested-With': 'XMLHttpRequest'
            };
        } else {
            console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
        }
    

提交回复
热议问题