Laravel 5.4:EloquentUserProvider.php中的JWTAuth,ErrorException

时间:2017-05-18 07:20:46

标签: laravel laravel-5.4

我是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的实例,实例   App \用户

这是我的用户模型:

<?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);
        }        
    }

在存储用户请求时,有效负载是密钥,值是json对象,下面给出了小样本对象:

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

1 个答案:

答案 0 :(得分:1)

将此添加到您的模型

use Illuminate\Foundation\Auth\User as Authenticatable;

并更改此行

class User extends Authenticatable

编辑: 看起来你在明文中存储密码。将其添加到您的用户模型。

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