[英]Laravel 8 : Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given
[英]Laravel API - Argument 1 passed to TokenGuard::__construct() must implement interface UserProvider
Laravel 8 中使用API 身份驗證的 REST-API。
介紹
我有Analytics
Model Authenticable 來驗證 Web 中的請求,而不是使用帶有相應analytics
和user
表的默認User
模型。 我已遷移analytics
表中的api_token
字段。 但是,在 POSTMAN 中訪問 API 路由時,我得到了以下錯誤響應。
回復
{
"message": "Argument 1 passed to Illuminate\\Auth\\TokenGuard::__construct() must implement interface Illuminate\\Contracts\\Auth\\UserProvider, null given, called in source\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManager.php on line 162",
"exception": "TypeError",
}
source\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php 在第 162 行
public function createTokenDriver($name, $config)
{
$guard = new TokenGuard(
$this->createUserProvider($config['provider'] ?? null),
$this->app['request'],
$config['input_key'] ?? 'api_token',
$config['storage_key'] ?? 'api_token',
$config['hash'] ?? false // **** This is line 162 **** //
);
我嘗試將第 162 行更改為$config['hash'] ?? true
$config['hash'] ?? true
,但仍然出現同樣的錯誤。
注意:
Analytics
和User
模型是Authenticable 。 雖然我在analytics
表中有api_token
字段
要求:
我正在端點 http://example.com/api/user?api_token=token 上發送 HTTP 請求的GET
實例( http://example.com/api/user?api_token=token(this is unhashed token)
下面是下面的配置。
route/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Analytics
和User
模型如下:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Notification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\AnalyticsResetPassword;
use Illuminate\Database\Eloquent\Model;
class Analytics extends Authenticatable
{
use Notifiable;
public function sendPasswordResetNotification($token)
{
$this->notify(new AnalyticsResetPassword($token));
}
protected $table = "analytics";
protected $fillable = ['name', 'email', 'password', 'mobile', api_token', ];
protected $hidden = ['password', 'api_token', 'remember_token', ];
}
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = ['name', 'email', 'password', ];
protected $hidden = ['password', 'remember_token',];
protected $casts = ['email_verified_at' => 'datetime',];
}
config/auth.php
配置文件中的guard
和provider
數組:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'analytics' => [
'driver' => 'session',
'provider' => 'analytics',
],
'api' => [
'driver' => 'token',
'provider' => 'user',
'hash' => true,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'analytics' => [
'driver' => 'eloquent',
'model' => App\Analytics::class,
],
],
Controller中的token生成方法
public function token(Request $request)
{
$token = Str::random(60);
$user = Auth::user();
$user->api_token = hash('sha256', $token);
$user->save();
return redirect('/analytics/security')->with('success', 'Token Generated Successfully!')->with("token" , $token);
}
'api' => [
'driver' => 'token',
'provider' => 'user',
'hash' => true,
],
我認為問題在於此保護配置中的提供者屬性,因為providers
中沒有user
條目-您只有users
和analytics
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.