[英]Laravel vapor "This action is unauthorized" with file upload
I have set up Laravel Vapor with my RESTFull app using Sanctum and now I'm trying to simply upload a file.我已经使用Sanctum通过我的 RESTFull 应用程序设置了 Laravel Vapor,现在我正在尝试简单地上传一个文件。 I'm making a request
POST /vapor/signed-storage-url
and I'm getting:我正在发出请求
POST /vapor/signed-storage-url
并且我得到:
{
"message": "This action is unauthorized.",
...
}
I have created the UserPolicy
as described in the docs :我已经按照文档中的描述创建了
UserPolicy
:
class UserPolicy
{
/**
* Determine whether the user can upload files.
*
* @param User $user
* @return bool
*/
public function uploadFiles(User $user): bool
{
return true;
}
}
But I keep getting This action is unauthorized
.但我不断收到
This action is unauthorized
。
The key piece of information here is that I'm using Sanctum to authenticate my users in my app.这里的关键信息是我正在使用Sanctum在我的应用程序中对我的用户进行身份验证。 Laravel's Vapor uses by default the
web
middleware Laravel 的 Vapor 默认使用
web
中间件
From the docs I wasn't able to find a way to publish Vapor's configuration.从文档中我无法找到发布 Vapor 配置的方法。
If we look at the routes configuration we will have:如果我们查看路由配置,我们将拥有:
/**
* Ensure that Vapor's internal routes are defined.
*
* @return void
*/
public function ensureRoutesAreDefined()
{
if ($this->app->routesAreCached()) {
return;
}
if (config('vapor.signed_storage.enabled', true)) {
Route::post(
config('vapor.signed_storage.url', '/vapor/signed-storage-url'),
Contracts\SignedStorageUrlController::class.'@store'
)->middleware(config('vapor.middleware', 'web'));
}
}
Vapor is getting the vapor.middleware
environment to tell which middleware will be applied to /vapor/signed-storage-url
route. Vapor 正在获取
vapor.middleware
环境以告知哪个中间件将应用于/vapor/signed-storage-url
路由。 Since I'm using Sanctum I just had to manually publish Vapor's configuration by creating a vapor.php
in my config
folder:因为我使用的是 Sanctum,所以我只需要通过在我的
config
文件夹中创建一个vapor.php
来手动发布 Vapor 的配置:
- config
-- app.php
-- filesystem.php
-- vapor.php 👈
Now in this file you can define your middleware to be set to auth:sanctum
:现在在这个文件中,您可以定义要设置为
auth:sanctum
的中间件:
<?php
return [
// Most of these variables are not necessary as the default from Vapor's
// core library is okay for most cases but I will leave here you need to use any of them
'redirect_to_root' => true,
'redirect_robots_txt' => true,
'serve_assets' => [],
'middleware' => 'auth:sanctum' 👈
];
Now Vapor will start to use the auth:sanctum
middleware to authenticate the request to POST /vapor/signed-storage-url
现在 Vapor 将开始使用
auth:sanctum
中间件来验证对POST /vapor/signed-storage-url
的请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.