gpt4 book ai didi

php - Nuxt 与 laravel Sanctuary 收到 "Unauthenticated"消息

转载 作者:行者123 更新时间:2023-12-05 05:01:53 28 4
gpt4 key购买 nike

我正在尝试将 Laravel sanctum 与 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

配置/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
},

登录.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>

最佳答案

在 nuxt.config.js 中,像这样检查本地服务器端口:

  auth: {
strategies: {
'laravelSanctum': {
provider: 'laravel/sanctum',
url: 'http://localhost:8000'
},
},
},

axios: {
baseURL: 'http://localhost:8000',
credentials: true
},

如果为真:

在 .env 文件中检查这个:

SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost:3000

然后检查你的 laravel kernel.php 文件,如果不存在则添加这个类:

'api' => [
EnsureFrontendRequestsAreStateful::class,
]

关于php - Nuxt 与 laravel Sanctuary 收到 "Unauthenticated"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62482172/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com