2017-05-18 15 views
0

私はlaravelの初心者ですので、間違いかもしれません。 laravelとtymondesigns/jwt-auth を使用してユーザーを確認します。私はこのチュートリアルComplete JWT-AUTH api with Laravelを見ていて、すべての手順、tymonパッケージのインストールとログインの手順に従っています。しかし、私はこのエラーが発生しています。私は以下のコードを投稿し、他のファイルからもっと多くのコードが必要かどうか教えてください。Laravel 5.4:JWTAuth、EloquentUserProvider.phpのErrorException

ErrorException in EloquentUserProvider.php line 120: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given

これは私のユーザモデルである:

<?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" 
          ]; 
    } 
?> 

私がUserControllerでこの私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); 
    } 
} 

ユーザーストア機能:

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

ユーザーを保存するには要求、ペイロードは鍵であり、v ALUEはJSONオブジェクトである、小さなサンプルオブジェクトは以下の通りです:

payload={ 
    "name": "Alexa", 
    "email": "[email protected]", 
"password":"12345", 
    "gender": "Male", 
    "age": 24 
} 
+0

このエラーに関連しないヒント:あなたの入力可能なフィールドから「id」を削除すると、あなたのIDを大量に割り当てることはできません。また、パスワードは隠し配列内になければなりません。充填可能なものではない。クレデンシャルの代わりにクレデンシャルを使用します。 – Christophvh

答えて

1

モデルに

use Illuminate\Foundation\Auth\User as Authenticatable; 

これを追加し、

class User extends Authenticatable 

この行を変更編集: は「あなたのように見えますパスワードを平文で保存する。あなたのユーザーモデルにこれを追加してください。

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

私もこれを試しました、エラーは削除されましたが、私は正しい資格情報を入力すると私はいつも無効な資格情報を取得します。 – MTA

+0

データベースにユーザーを追加するコードを共有します。私はあなたが平文のパスワードを直接保管していると思います。デフォルトでは、laravelのauthメソッドは比較のためにパスワード入力をハッシュします。 – Sandeesh

+0

私は投稿を更新しました。上記のどのように私がデータベースにユーザを追加しているかを見ることができます。 – MTA

関連する問題