php - Laravel API - 传递给 TokenGuard::__construct() 的参数 1 必须实现接口 UserProvider
问题描述
Laravel 8 中使用API 身份验证的 REST-API 。
介绍
我有Analytics
Model Authenticable 来验证 Web 中的请求,而不是使用User
带有相应表analytics
和user
表的默认模型。我已经迁移api_token
了analytics
表中的字段。但是,在 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
但仍然出现相同的错误。
注意:和
Analytics
ModelUser
是Authenticable。虽然我在表中有api_token
字段analytics
要求:
我正在GET
端点上发送 HTTP 请求实例
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',];
}
配置文件中的guard
andprovider
数组:config/auth.php
'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);
}
解决方案
推荐阅读
- javascript - 如何在 jscodeshift 中包装除导入之外的所有顶级语句和声明?
- image - Unable to render image on UI in jsp page of Spring MVC Application
- python - Merge multiple dataframes - how to assign column names automatically?
- soapui - READYAPI - Run multiple data per iteration on datasource
- android - Bazel + Android + Java8 complains about non-referenced API level 13
- sql - 下一篇:jdbc 如何允许 SQL Server 多条语句
- javascript - 如何按id过滤和获取数组?
- javascript - 在不使用 jquery 的情况下,如何在用户滚动 100px 后显示 div?
- ios - 我们可以检查允许应用程序请求跟踪切换是否打开
- c# - 如何使用 SocketIOClient 将 C# 连接到 SocketIO