2016-10-22 6 views
1

dingo/apijwt-authをサポートしています)を使用してAPIを作成しています。dingo/apiでjwt-authで無効なトークンエラーで認証できません

これは私のルートであると仮定しますAUTHORIZEのタスクを持ち、トークンを作成するようなものです

$api->group(['prefix' => 'auth', 'namespace' => 'Auth'], function ($api) { 
      $api->post('checkPhone', '[email protected]'); 

      //Protected Endpoints 
      $api->group(['middleware' => 'api.auth'], function ($api) { 
       $api->post('sendCode', '[email protected]'); 
       $api->post('verifyCode', '[email protected]'); 

      }); 
     }); 

checkPhone方法:Userモデルの

public function checkPhone (Request $request) 
     { 
      $phone_number = $request->get('phone_number'); 
      if (User::where('phone_number', $phone_number)->exists()) { 

       $user = User::where('phone_number', $phone_number)->first(); 

       $user->injectToken(); 

       return $this->response->item($user, new UserTransformer); 

      } else { 
       return $this->response->error('Not Found Phone Number', 404); 
      } 
     } 

そしてinjectToken()方法は次のとおりです。

public function injectToken() 
     { 
      $this->token = JWTAuth::fromUser($this); 
      return $this; 
     } 

トークン作成が動作します。 e。

しかし、保護されたエンドポイントに送信すると、常にUnable to authenticate with invalid tokenが発生します。

保護されたエンドポイントのアクションメソッドは次のとおりです。

public function verifyCode (Request $request) 
     { 
      $phone_number = $request->get('phone_number'); 
      $user_code = $request->get('user_code'); 

      $user = User::wherePhoneNumber($phone_number)->first(); 

      if ($user) { 
       $lastCode = $user->codes()->latest()->first(); 

       if (Carbon::now() > $lastCode->expire_time) { 
        return $this->response->error('Code Is Expired', 500); 
       } else { 
        $code = $lastCode->code; 

        if ($user_code == $code) { 

         $user->update(['status' => true]); 

         return ['success' => true]; 
        } else { 
         return $this->response->error('Wrong Code', 500); 
        } 
       } 
      } else { 
       return $this->response->error('User Not Found', 404); 
      } 
     } 

私はAPIクライアントとしてPostManを使用して、このようなヘッダとして生成されたトークンを送信します。

Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5ODkxMzk2MTYyNDYiLCJpc3MiOiJodHRwOlwvXC9hcGkucGFycy1hcHAuZGV2XC92MVwvYXV0aFwvY2hlY2tQaG9uZSIsImlhdCI6MTQ3NzEyMTI0MCwiZXhwIjoxNDc3MTI0ODQwLCJuYmYiOjE0NzcxMjEyNDAsImp0aSI6IjNiMjJlMjUxMTk4NzZmMzdjYWE5OThhM2JiZWI2YWM2In0.EEj32BoH0URg2Drwc22_CU8ll--puQT3Q1NNHC0LWW4 
私は上の多くの検索後に解決策を見つけることができません

ウェブと関連するリポジトリ。

あなたの意見は、問題がある場合、

更新:

public function __construct() 
     { 
      $this->middleware('guest', ['except' => 'logout']); 
     } 

私は$this->middleware('guest', ['except' => 'logout']);をコメントしたときに、すべての物事が働いていたので:

私が見つからないエラーがlaravelが提供するloginControllerのコンストラクタのためであることがわかりました。 しかし、私はこの行を削除する場合は正しいですか? この行はどのようにAPIにする必要がありますか?

+0

$ API->バージョン(V1 ''、[ 'ミドルウェア' => 'api.auth']、関数($ API){ $ API->取得( 'ユーザ'、関数(){ $ user = app( 'Dingo \ Api \ Auth \ Auth') - > user(); return $ user; }); });このような認証されたユーザを確認してください。 –

+0

@ Jagadesha NH、それを試しましたが、「不正な資格情報または不正な認証ヘッダのために認証に失敗しました」というエラーが発生しました。 –

+0

https://github.com/dingo/api/issues/325 https://github.com/tymondesigns/jwt-auth/issues/16これらのリンクを確認してください。 –

答えて

0

前述のUpdate noteの問題は、LoginControllerでcheckPhoneverifyCodeを使用していました。これは、そのコンストラクタにguestのチェックがあります。

そしてguestミドルウェアは\App\Http\Middleware\RedirectIfAuthenticated::classを参照し、それが/homeディレクトリにログインしているユーザーリダイレクトし、私はそう404 errorが発生した、ことを作成していなかったため。

ここでは、そのメソッドをコンストラクタ内にミドルウェアなしでUserControllerに移動しました。これに私のconfig/api.phpを更新

0

は、トリック起こっていただきました!見にソースを読ん常に価値

// config/api.php 
 
... 
 
    'auth' => [ 
 
     'jwt' => 'Dingo\Api\Auth\Provider\JWT' 
 
    ], 
 
...

0

をしました。回答:ユーザーを取得するために、認証プロバイダの識別子が必要です。

/** 
* Authenticate request with a JWT. 
* 
* @param \Illuminate\Http\Request $request 
* @param \Dingo\Api\Routing\Route $route 
* 
* @return mixed 
*/ 
public function authenticate(Request $request, Route $route) 
{ 
    $token = $this->getToken($request); 

    try { 
     if (! $user = $this->auth->setToken($token)->authenticate()) { 
      throw new UnauthorizedHttpException('JWTAuth', 'Unable to authenticate with invalid token.'); 
     } 
    } catch (JWTException $exception) { 
     throw new UnauthorizedHttpException('JWTAuth', $exception->getMessage(), $exception); 
    } 

    return $user; 
} 
関連する問題