2017-02-07 5 views
0

私はhttps://github.com/tymondesigns/jwt-authを使用しています。ここでTymon JWTの代わりにデータベースプロバイダを使用して

は、私がインストールする方法についてのチュートリアルを読んだ私のcomposer.json

"laravel/lumen-framework": "5.3.*", 
"tymon/jwt-auth": "^[email protected]", 

です。そのうちのいくつかは以下のとおりです。https://laravelista.com/posts/json-web-token-authentication-for-lumen

https://scotch.io/tutorials/role-based-authentication-in-laravel-with-jwt

    • は、私はそれが私の地元に取り組み、成功したトークンを返す作ることができています。しかし、問題は、database.sqliteからデータを取得するプロバイダに雄弁を使用する代わりに、私のドライバとしてデータベースを使いたいということです。それと

      、私はそれが今でデータベーススルー接続であるので、それは今私もいくつかのコードを変更する必要がありDatabaseUserProvider.php

      を使用して、私のconfig/auth.php

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

      を設定しています。

      /** 
      * Validate a user against the given credentials. 
      * 
      * @param \Illuminate\Contracts\Auth\Authenticatable $user 
      * @param array $credentials 
      * @return bool 
      */ 
      public function validateCredentials(UserContract $user, array $credentials) 
      { 
          $plain = $credentials['password']; 
      
          return $this->hasher->check($plain, app('hash')->make($user->getAuthPassword())); 
      } 
      

      パスワードを検証するときにapp('hash')->make()を追加したことに注目してください。

      次に、取得したユーザーをGenericUserオブジェクトに挿入します。

      /** 
      * Get the generic user. 
      * 
      * @param mixed $user 
      * @return \Illuminate\Auth\GenericUser|null 
      */ 
      protected function getGenericUser($user) 
      { 
          if (! is_null($user)) { 
           return new GenericUser((array) $user); 
          } 
      } 
      

      それはGenericUserオブジェクト上にあるので、それはのエラー与える:この問題を解決するために

      Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of Illuminate\Auth\GenericUser given 
      

      を、私は下のすべての方法にJWTSubject注入を除去することによって、それを「ハック」する必要がありますtymon\jwt-auth\src\JWT.php

      これをクリーニングするより良い方法はありますか?

  • 答えて

    0

    認証ユーザーに、最も適切なJWTSubjectを実装させることで、簡単に修正できます。

    class GenericUser implements JWTSubject { 
        [...] 
    } 
    

    しかし、あなたはLaravelネイティブ実装を扱っていることから、私はそれを拡張するためにあなたを示唆してJWTSubjectを実装して、代わりにJWT-AUTHのコードでどこでも行くのと、型のヒントを削除したいです。

    関連する問題