[英]How can I use middleware to authorize my laravel Restfull API
我正在使用 laravel/passport 並且我想授權我的 API 端點然后我收到的結果聲明一個路由(用戶/登錄)沒有在郵遞員中定義為狀態碼 500
Route::get('all/users', 'UserController@index')->middleware('auth');
{
return new UserResource(User::findOrFail($id));
}
use Laravel\Passport\HasApiTokens;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
protected $fillable = [
'name', 'email', 'password','balance','phone'
];
protected $hidden = [
'password', 'remember_token',
];
}```
當您在web.php
文件中調用Auth::routes()
時,Route user/login
在您的應用程序中聲明。
返回該錯誤是因為您正在使用api
路由並且在其上調用了auth
中間件。
如果您只想在未經授權的用戶嘗試訪問 api 時返回401 unauthorized
響應,則必須使用帶有api
防護的auth
中間件。
請參閱下面的示例和正確的代碼:
Route::get('all/users', 'UserController@index')->middleware('auth:api');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.