laravel护照未定义认证用户提供者[passport]

时间:2018-10-23 08:27:35

标签: php laravel authentication oauth-2.0 laravel-passport

我在laravel中有一个api,我使用护照进行身份验证。

在我的config / auth.php文件中,我已对此进行了更改,因此api卫队使用了护照并指向了我的用户提供程序

'guards' => [
   'web' => [
       'driver' => 'session',
       'provider' => 'users',
   ],

   'api' => [
       'driver' => 'passport',
       'provider' => 'users',
   ],
],

'providers' => [
   'users' => [
       'driver' => 'passport',
       'model' => App\Users::class,
   ],

这是我的App / user.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public static function lookup($login)
    {
        return Self::where('email', $login)->first();
    }
}

现在,当我想登录某人时,我运行以下代码:

$newUser = new User([
           'name' => $login,
           'email' => $login,
           'password' => $password
       ]);

       $authUser = User::lookup($login);

       $tokenResult = $authUser->createToken('Personal Access Token');
       $token = $tokenResult->token;

       if ($remember)
       {
          $token->expires_at = Carbon::now()->addWeeks(1);
          $token->save();
       }

       return response()->json([
          'access_token' => $tokenResult->accessToken,
          'token_type' => 'Bearer',
          'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString()
        ]);

这可以向我发送访问令牌。我要尝试做的是检查用户在网站上执行其他功能时是否已登录。我在下面的路由文件中这样做:

Route::get('me', 'Account@me')->middleware('auth:api');

所以我想获取任何登录者的信息,但是通过给我一个错误“验证用户提供者[未定义密码]”错误,它在中间件上崩溃了

需要任何帮助

1 个答案:

答案 0 :(得分:0)

您使用了错误的passport用户提供程序驱动程序,而是使用eloquent,如下所示:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
],

此外,模型名称中还有一个s