2016-03-04 43 views
2

當談到訪問我登錄我有這樣的錯誤:登錄失敗在laravel 5(JTW)

{"error":"token_not_provided"} 

我有我的.htaccess:

RewriteEngine On 
RewriteCond %{HTTP:Authorization} . 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

我在我的路線。 PHP:

Route::post('/api/login', '[email protected]'); 
Route::get('/api/login', '[email protected]'); 

,我有我的AuthenticateController:

use JWTAuth; 
use Tymon\JWTAuth\Exceptions\JWTException; 

class AuthenticateController extends Controller 
{ 
    public function authenticate(Request $request) 
    { 
     // grab credentials from the request 
     $credentials = $request->only('email', 'password'); 

     try { 
      // attempt to verify the credentials and create a token for the user 
      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')); 
    } 

你能幫我嗎?非常感謝你的問候

+0

你可以看到你的瀏覽器發送頭,並有類似:'授權:Bearer' +長鍵? –

+0

這是否幫助http://stackoverflow.com/questions/33101810/getting-token-not-provided-when-using-authorization-header-with-jwt-laravel – haakym

回答

1

你需要確保你的觀點有:{! csrf_field()!!},你不需要將它作爲表單中的隱藏字段。

<form class="form-horizontal" method="post"> 
 

 
    @foreach ($errors->all() as $error) 
 
    <p class="alert alert-danger">{{ $error }}</p> 
 
    @endforeach 
 
    
 
    {!! csrf_field() !!} 
 

 
    <fieldset>

+0

非常感謝您設法修復了以下問題: Route :: group(['middleware'=> ['jwt.auth','jwt.refresh']],function(){ \t Route :: post('logout','Api \ AuthController @ logout'); \t路線::得到( '試驗',函數(){ \t \t返回響應() - > JSON([ '富'=> '巴']); \t});和此功能之內的所有的功能使提到登錄。再次感謝,問候 – Lucia

1

你的觀點在哪裏?我認爲你需要在你的刀片上加入一些東西。

<input type="hidden" name="_token" value="{{ csrf_token() }}"> 

如果您沒有使用視圖,並且您正在從外部請求執行此操作。然後,你可以禁用csrf_token()

這裏是一個鏈接How to disable Middlware CSRF in Laravel