2017-03-03 11 views
0

私はLaravel APIプロジェクトを作成しようとしています。だから私は基本的なlaravelの足場が設定されたこのプロジェクトを持っています。私が追加した私のユーザーの移行では:Laravel api authorization with api_token

$table->string('api_token', 60)->unique(); 

を、その後、私のUser.phpモデルに私が追加しました:

protected $fillable = [ 
    'name', 'email', 'password','api_token' 
]; 

はその後、私のapi.phpに私がテストルートを作っています

ルート::グループ([ 'ミドルウェア' => [ 'AUTH:API']私Apicontrollerで、関数(){

Route::get('/test', '[email protected]'); 

}); 

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

class ApiController extends Controller 
{ 


public function test(Request $request){ 

return response()->json(['name' => 'test']); 


} 



} 

だから今はこのように入力します。役立つだろう私のapi_token

localhost/project1/public/api/test?api_token='hsvdvhvsjhvasdvas8871238' 

でそれは、私にJSONデータを与えていない代わりに、ホーム・ページにログインし

+0

localhost/project1/testを試してみませんか?返信のために – SteD

+0

thx SteDがそれを修正しました。その愚かな間違いを私は作った。ルートに/ apiは含まれていませんでした。しかし、今私は別の問題が私の編集された質問を参照してください。 – Mikethetechy

+1

@SteD申し訳ありませんが、再びその愚かな間違いです。私はそれを修正した:)再び申し訳ありませんが、私の悪いはもう少し患者の笑をされている必要があります。 – Mikethetechy

答えて

1

localhost/project1/public/index.php/api/test?api_token='hsvdvhvsjhvasdvas8871238'にリダイレクトます。あなたはLaravel 5.3以降のバージョンを使用する場合は、独自のAPIのミドルウェアやルートを記述する必要はないだろうPretty URLs

+0

thxx @gentcys。 – Mikethetechy

+0

GETパラメータでトークンを送信するのは安全な方法ではありません。返信のために –

0

あなたはかなりのURLをしたい場合は

は、ドキュメントを読んでください。

また、内蔵のPassportパッケージを使用して、oAuth2を使用してアクセストークンを管理できます。 laravel 5.2
ミドルウェア/ ApiAuthenticateについて

$http = new GuzzleHttp\Client; 

$response = $http->post($apiUrl.'oauth/token', [ 
    'form_params' => [ 
     'grant_type' => 'password', 
     'client_id' => '2', //this can be generated when you setup Passport package or using artisan commands 
     'client_secret' => 'xxxxxxxxx', //this can be generated when you setup Passport package or using artisan commands 
     'username' => '[email protected]', 
     'password' => 'test123', 
     'scope' => '', 
    ], 
]); 

$responseData = json_decode($response->getBody(), true); 

$token = $responseData['access_token']; //Now I have the token so I can call any protected routes 

$response = $http->request('GET', $apiUrl.'api/v1/user', [ 
    'headers' => [ 
     'Accept' => 'application/json', 
     'Authorization' => 'Bearer '.$token, 
    ], 
]); 

$responseData = json_decode($response->getBody(), true); 
echo "Name of the user is: ".$responseData['name']; 
+0

Thx tanay jha。私はパスポートの書類に従ってみました。それはちょっと難しいです、それはvue.jsとgulpフロントエンドのものについて話しています。それはかなり混乱しています。 – Mikethetechy

+0

@MohamedManas Vue.jsを使用する必要はありません。フロントエンドをVue.jsベースにしたい場合にのみ必要です。私の編集された答えを見てください。説明のために –

+0

thxです。よく見える。やってみます – Mikethetechy

0

namespace App\Http\Middleware; 

use Closure; 
use Illuminate\Support\Facades\Auth; 

class ApiAuthenticate 
{ 

    public function handle($request, Closure $next, $guard = null) 
    { 
     if (Auth::guard($guard)->guest()) { 
      return response()->json(['status'=>'error','message'=>'token mismatch']);; 
     } 
     return $next($request); 
    } 
} 


Kernel.php追加

protected $routeMiddleware = [ 
    'autho'  => \App\Http\Middleware\ApiAuthenticate::class, 
]; 


routes.phpの

 

    Route::group(['prefix'=>'api','middleware'=>'autho:api'], function(){ 
     Route::get('aaa','Api\[email protected]'); 
    });