簡體   English   中英

Laravel API - 傳遞給 TokenGuard::__construct() 的參數 1 必須實現接口 UserProvider

[英]Laravel API - Argument 1 passed to TokenGuard::__construct() must implement interface UserProvider

Laravel 8 中使用API 身份驗證的 REST-API。

介紹

我有Analytics Model Authenticable 來驗證 Web 中的請求,而不是使用帶有相應analyticsuser表的默認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 ,但仍然出現同樣的錯誤。

注意: AnalyticsUser模型是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();
});

AnalyticsUser模型如下:

<?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配置文件中的guardprovider數組:

    '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條目-您只有usersanalytics

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM