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