带有laravel密室的Nuxt收到“未经身份验证”消息

时间:2020-06-20 05:49:21

标签: php laravel api vue.js nuxt.js

我正在尝试将Laravel圣所与NuxtJS一起使用。问题是我能够通过get csrf并登录,但是当我尝试访问api /用户时,我收到“未授权”消息。他们说我应该将SESSION_DRIVER设置为cookie,但还是一样。我正在使用xampp,所以我在 localhost:8000 上运行了laravel,并在 localhost:3000 上运行了nuxtjs。预先感谢。

Unauthenticated

Laravel API

.env

SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost

config / cors

'paths' => [
    'api/*',
    '/login',
    '/logout',
    '/sanctum/csrf-cookie'
],
'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => true,

api.php

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
   return $request->user();
});

NUXT JS

nuxt.config.js

axios: {
     baseURL: "http://localhost:8000/",
     credentials: true
},
auth: {
   redirect: {
       login: '/login',
       logout: '/',
       callback: '/login',
       home: '/'
   },
   strategies: {
          local: {
             endpoints: {
                login: { url: '/login', method: 'post', propertyName: false },
                user: { url: '/api/user', method: 'get', propertyName: false }
             },
             tokenRequired: false,
             tokenType: false
          }
   },
   localStorage: false
},

Login.vue

export default {
data(){
    return{
        email:'',
        password:''
    }
},
components: {

},
methods:{
    login(){
        this.$axios.get('/sanctum/csrf-cookie', {
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            },
            withCredentials: true,
        })
        .then( function(){
            this.$auth.loginWith('local', {
                data: {
                    email: this.email,
                    password: this.password
                },
            });
        }.bind(this))
    }
},
mounted(){  
}
}
</script>

0 个答案:

没有答案