2016-03-18 14 views
1

私はlaravel/lumenフレームワークでかなり新しいです。私は、快適なAPIを構築するためにルーメン5.2を使用しています。認証のために、私はこのhttps://laravelista.com/json-web-token-authentication-for-lumenのガイダンスの記事に従っているJWT認証を実装しようとしています。私はこのhttps://github.com/tymondesigns/jwt-auth パッケージをインストールして設定します。それは正常に動作し、私はトークン{"エラー": "token_not_provided"}を提供していない場合、次のエラーを与えます。しかし、私はポストリクエストで電子メールとパスワードを渡してトークンを生成しようとしているときに失敗し、 。ここでJWT認証ルーメンのエラー5.2

in AuthManager.php line 137 
at Application->Laravel\Lumen\Concerns\{closure}('8', 'Undefined index: provider', 'D:\xamp\htdocs\lumen_api\vendor\illuminate\auth\AuthManager.php', '137', array('name' => 'api', 'config' => array('driver' => 'token'))) in AuthManager.php line 137 
at AuthManager->createTokenDriver('api', array('driver' => 'token')) in AuthManager.php line 77 
at AuthManager->resolve('api') in AuthManager.php line 57 
at AuthManager->guard() in AuthManager.php line 244 
at AuthManager->__call('once', array(array('email' => '[email protected]', 'password' => 'password'))) in IlluminateAuthAdapter.php line 39 
at AuthManager->once(array('email' => '[email protected]', 'password' => 'password')) in IlluminateAuthAdapter.php line 39 
at IlluminateAuthAdapter->byCredentials(array('email' => '[email protected]', 'password' => 'password')) in JWTAuth.php line 108 
at JWTAuth->attempt(array('email' => '[email protected]', 'password' => 'password')) in Facade.php line 216 
at Facade::__callStatic('attempt', array(array('email' => '[email protected]', 'password' => 'password'))) in AuthController.php line 45 
at JWTAuth::attempt(array('email' => '[email protected]', 'password' => 'password')) in AuthController.php line 45 
at AuthController->postLogin(object(Request)) 
at call_user_func_array(array(object(AuthController), 'postLogin'), array(object(Request))) in Container.php line 507 
at Container->call(array(object(AuthController), 'postLogin'), array()) in RoutesRequests.php line 581 
at Application->callControllerCallable(array(object(AuthController), 'postLogin'), array()) in RoutesRequests.php line 548 
at Application->callLumenController(object(AuthController), 'postLogin', array(true, array('uses' => 'App\Http\Controllers\[email protected]'), array())) in RoutesRequests.php line 521 
at Application->callControllerAction(array(true, array('uses' => 'App\Http\Controllers\[email protected]'), array())) in RoutesRequests.php line 489 
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\Http\Controllers\[email protected]'), array())) in RoutesRequests.php line 474 
at Application->handleFoundRoute(array(true, array('uses' => 'App\Http\Controllers\[email protected]'), array())) in RoutesRequests.php line 376 
at Application->Laravel\Lumen\Concerns\{closure}() in RoutesRequests.php line 624 
at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php line 382 
at Application->dispatch(object(Request)) in RoutesRequests.php line 327 
at Application->run(object(Request)) in index.php line 29 

私Authcontrollerコードは次のとおりです。

namespace App\Http\Controllers; 

use Illuminate\Http\Exception\HttpResponseException; 
use JWTAuth; 
use Tymon\JWTAuth\Exceptions\JWTException; 
use Illuminate\Http\Request; 
use Illuminate\Http\Response as IlluminateResponse; 

class AuthController extends Controller{ 


/** 
* Handle a login request to the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function postLogin(Request $request) 
{ 
    try 
    { 
     $this->validate($request, [ 
      'email' => 'required|email|max:255', 'password' => 'required', 
     ]); 
    } 
    catch (HttpResponseException $e) 
    { 
     return response()->json([ 
      'error' => [ 
       'message'  => 'Invalid auth', 
       'status_code' => IlluminateResponse::HTTP_BAD_REQUEST 
      ]], 
      IlluminateResponse::HTTP_BAD_REQUEST, 
      $headers = [] 
     ); 
    } 

    $credentials = $this->getCredentials($request); 

    try 
    { 
     // attempt to verify the credentials and create a token for the user 
     //$customClaims = ['email' => '[email protected]', 'password' => 'password']; 
     if (! $token = JWTAuth::attempt($credentials)) 
     { 
      return response()->json(['error' => 'invalid_credentials'], 401); 
     } 
    } 
    catch (JWTException $e) 
    { 
     // something went wrong whilst attempting to encode the token 
     return response()->json(['error' => 'could_not_create_token'], 500); 
    } 

    // all good so return the token 
    return response()->json(compact('token')); 
} 

/** 
* Get the needed authorization credentials from the request. 
* 
* @param \Illuminate\Http\Request $request 
* @return array 
*/ 
protected function getCredentials(Request $request) 
{ 
    return $request->only('email', 'password'); 
} 

}

========================= ==========
私.envファイルの内容は

APP_ENV=local 
APP_DEBUG=true 
[email protected] 

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_PORT=3306 
DB_DATABASE=api_db 
DB_USERNAME=root 
DB_PASSWORD= 

CACHE_DRIVER=memcached 
QUEUE_DRIVER=sync 

JWT_SECRET=cv4d4se065r1td0sw6e8d9za9q102jhes060a3wer 

AUTH_DRIVER=jwt 
AUTH_MODEL=\App\Models\User 
AUTH_TABLE=users 

私はそれは多くのことをグーグルが、まだすべてのソリューションを取得できません。それを理解するのを助けてください。

ありがとうございます。ここで

は、あなたが(それが存在しない場合は、あなた自身で作成する場合があります) config/auth.phpで独自の auth設定ファイルを書き込むこと

![][vender folder]

+0

あなたのディレクトリ構造を示しています。特にJWT Authが公開されているベンダーのフォルダ –

+0

また、 'php artisan jwt:generate'を実行してみて、あなたが見ている応答が何であるかを確認してください –

+0

@ Vishal Shありがとうございます、私はコマンドphp artisan jwtを実行します:メッセージ[トークン]は正常に設定されましたが、私の.envファイルにどこに格納されているのか分かりませんが、トークン値に変更はなく、同じエラーが発生します。私は私の質問に私のディレクトリ構造を追加しています。 –

答えて

1

ベンダーフォルダのディレクトリ構造です。設定hereを参照してください。

<?php 

return [ 

    /* 
    |-------------------------------------------------------------------------- 
    | Authentication Defaults 
    |-------------------------------------------------------------------------- 
    | 
    | This option controls the default authentication "guard" and password 
    | reset options for your application. You may change these defaults 
    | as required, but they're a perfect start for most applications. 
    | 
    */ 

    'defaults' => [ 
     'guard' => env('AUTH_GUARD', 'api'), 
    ], 

    /* 
    |-------------------------------------------------------------------------- 
    | Authentication Guards 
    |-------------------------------------------------------------------------- 
    | 
    | Next, you may define every authentication guard for your application. 
    | Of course, a great default configuration has been defined for you 
    | here which uses session storage and the Eloquent user provider. 
    | 
    | All authentication drivers have a user provider. This defines how the 
    | users are actually retrieved out of your database or other storage 
    | mechanisms used by this application to persist your user's data. 
    | 
    | Supported: "session" 
    | 
    | NOTE: "token" driver is not supported in JWT Auth 
    | 
    */ 

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

    /* 
    |-------------------------------------------------------------------------- 
    | User Providers 
    |-------------------------------------------------------------------------- 
    | 
    | All authentication drivers have a user provider. This defines how the 
    | users are actually retrieved out of your database or other storage 
    | mechanisms used by this application to persist your user's data. 
    | 
    | If you have multiple user tables or models you may configure multiple 
    | sources which represent each model/table. These sources may then 
    | be assigned to any extra authentication guards you have defined. 
    | 
    | Supported: "database", "eloquent" 
    | 
    */ 

    'providers' => [ 
     'users' => [ 
      'driver' => 'eloquent', 
      // We should get model name from JWT configuration 
      'model' => app('config')->get('jwt.user'), 
     ], 
    ], 

]; 

Fortunately, I create a simple JWT Authentication implemented in Lumen here .

+0

答えがうまくいけば、ありがとう。しかし、ガウドで「api」の代わりに「セッション」ドライバを使用する理由を教えてください。なぜあなたはテーブル "password_resets"を使用しているのか説明できますか? 再度お返事ありがとうございます。 –

+0

こんにちはKrisan、私は@ user3273700と同じ質問があります、なぜあなたはここでセッションを使用していますか? JWTの目的はセッションを使わないことだと思った。 'session'を' jwt'に編集しようとすると、 'Auth guard driver [api]が定義されていません.' –

+0

こんにちは。ですから、私はライブラリを掘り下げ、Guardが 'once'と' onceUsingId'を実装しなければならないことを発見しました。これは 'Illuminate \ Contracts \ Auth \ StatefulGuard'で宣言されています。残念ながら、この契約を実装した 'SessionGuard'だけです。だから、私はセッションを使用しています。しかし、心配はありません。セッションは保存されていません。実装が必要なだけです。 'file'セッションドライバを使用している時を見ることができます。 'storage/sessions'ディレクトリにはセッションファイルがありません。後で、私は自己信者のAuthGuardを作成することによってそれを変更します。 – Alfa