首页 > 解决方案 > Laravel API - 传递给 TokenGuard::__construct() 的参数 1 必须实现接口 UserProvider

问题描述

Laravel 8 中使用API 身份验证的 REST-API 。

介绍

我有AnalyticsModel Authenticable 来验证 Web 中的请求,而不是使用User带有相应表analyticsuser表的默认模型。我已经迁移api_tokenanalytics表中的字段。但是,在 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但仍然出现相同的错误。

注意:AnalyticsModelUserAuthenticable。虽然我在表中有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',];
}

配置文件中的guardandprovider数组: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);
    }

标签: phplaravelapirestauthentication

解决方案


推荐阅读