【问题标题】:Laravel 5.4: JWTAuth, ErrorException in EloquentUserProvider.phpLaravel 5.4:EloquentUserProvider.php 中的 JWTAuth、ErrorException
【发布时间】:2017-10-17 21:04:12
【问题描述】:

我是 laravel 的新手,所以这可能是我的错误。使用 laravel 和 tymondesigns/jwt-auth 来验证用户。我正在观看本教程Complete JWT-AUTH api with Laravel 并遵循每一步,tymon 包安装和登录用户。但我收到了这个错误。我在下面发布了代码,如果您需要任何其他文件中的更多代码,请告诉我。

EloquentUserProvider.php 第 120 行中的 ErrorException:参数 1 已通过 到 Illuminate\Auth\EloquentUserProvider::validateCredentials() 必须是 Illuminate\Contracts\Auth\Authenticatable 的一个实例, 应用\用户给定

这是我的用户模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class User extends Model
    {
        protected $hidden = ["password"];
        protected $fillable = [
                                "id",
                                "name",
                                "password",
                                "mobile_number",
                                "gender",
                                "age",
                                "company_name",
                                "profile_image",
                                "email"
                            ];
    }
?>

这是我的 ApiAuthController.php

use JWTAuth;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;

class ApiAuthController extends Controller
{
    public function authenticate(){
        $credentaials = request()->only('email', 'password');
        print_r($credentaials);

        try {
            $token = JWTAuth::attempt($credentaials);
            if(!$token){
                return response()->json(['error'=>'invalid credentaials'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error'=>'something went wrong'], 500);
        }
        return response()->json(['token'=>$token], 200);
    }
}

我的 UsersController 中的用户存储功能:

public function store(Request $request)
    {
        $payload = json_decode($request->payload, true);

        $validator = Validator::make($payload, $this->rules);

        if ($validator->passes()) {

            $user = (new User())->fill($payload);
            if($user->save()){

                $response = [
                    "msg" => "User created",
                    "link" => "/api/users/" . $user->id
                ];
                return response()->json($response, 200);
            }

            $response = [
                "msg" => "An error occured"
            ];

            return response()->json($response, 404);

        } 
        else {
            return response()->json($validator->messages(), 404);
        }        
    }

在存储用户请求时,payload为key,value为json对象,小样本对象如下:

payload={
  "name": "Alexa",
  "email": "alexa@gmail.com",
"password":"12345",
  "gender": "Male",
  "age": 24
}

【问题讨论】:

  • 与此错误无关的提示:从可填写字段中删除“id”,您不希望您的 id 可批量分配。而且您的密码应该只在隐藏数组中。不是在可填充的。以及它的 Credentials 而不是 Credentials。

标签: laravel laravel-5.4


【解决方案1】:

将此添加到您的模型中

use Illuminate\Foundation\Auth\User as Authenticatable;

并改变这一行

class User extends Authenticatable

编辑: 看起来您正在以明文形式存储密码。将此添加到您的用户模型中。

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

【讨论】:

  • 我也试过了,错误已被删除,但如果我输入正确的凭据,我总是得到无效的凭据。
  • 在您将用户添加到数据库的位置共享您的代码。我猜你是直接存储明文密码。默认情况下,laravel 的 auth 方法对密码输入进行哈希处理以进行比较。
  • 我已经更新了帖子,你可以在上面看到我是如何在数据库中添加用户的。
  • @MTA 我编辑了我的答案,检查一下。您以明文存储密码而不是存储密码哈希,但是当您使用 JWT::attempt() 时,它又使用 laravel 内置的 auth 方法来比较输入密码的哈希。因此,您会收到无效的凭据错误。添加我显示的 setPasswordAttribute。除了您链接的视频系列的第 5 集外,您还可以使用 bcrypt hasher。不知道你是怎么错过的。
猜你喜欢
  • 2017-09-24
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2016-08-08
  • 2019-07-20
  • 2017-10-29
相关资源
最近更新 更多