4

I am trying to pass the token that I have in my Java Script controller to authenticate to an API in Laravel. From the headers of my controller, I define an '' Authorization ':' Bearer '+ $ scope.token "to be sent to the API. But unfortunately I'm encountering this error:

"token_not_provided"

Route:

Route::get('/members', 'PessoaController@index')->middleware('jwt.auth');

I'm use JWTAuth.

My Controller API:

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\PessoaRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;

class PessoaController extends Controller
{

protected $user;

    public function __construct(\Illuminate\Http\Request $request)
{
    $header = $request->header('Authorization');

    return response([
        'status' => 'success'
    ]) ->header('Authorization', $header);
}

Angular Controller:

(function() {
 app.controller('MemberController', [
'$scope', '$rootScope', 'AppConfig', '$http',
function($scope, $rootScope, AppConfig, $http) {
  var vm = this;
  $rootScope.page.title = 'SEC BASE';

  $rootScope.authenticated = true;
  $scope.token = $rootScope.user.remember_token;
   getMembers();

    // Obtem a listagem de todos os usuarios
  function getMembers() {
      Loading.show();
    $http({
        headers: {
            'Content-Type' : 'application/json',
            'Authorization' : 'Bearer ' + token
        },
        method: 'GET',
        url: AppConfig.ApiUrl + 'members'
    }).then( response => {
      // console.log(response);
      $scope.members = response.data;
    }).finally(Loading.hide)
        .catch(function(err) {
            console.log(err);
        });
  }
  }
  ]);
})();

Handler.php:

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Tymon\JWTAuth\Facades\JWTAuth;
public function render($request, Exception $exception)
{
    // detect instance
    try {
        $user = JWTAuth::parseToken()->authenticate();
    } catch (Exception $e) {
        if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
            return $this->respondWithError("Token is Invalid");
        }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
            return $this->respondWithError(['error'=>'Token is Expired']);
        }else{
            return $this->respondWithError(['error'=>'Something is wrong']);
        }
    }
    return $next($request);
}

Error return:

enter image description here

Vinícius Matos
  • 144
  • 1
  • 9

2 Answers2

0

The way you build the request looks good. It looks like the error comes from the API server that does not receive the token.

If you are running your PHP application using Apache, then you should have a look at this question and the accepted answer.

Apache does not correctly serves the authorization header to the applications. A dedicated rule have to be set in you .htaccess to get it.

Spomky-Labs
  • 15,473
  • 5
  • 40
  • 64
0

If Bearer is not working then you have to send token with parameter data

Not in headers. i also have faced this issue and fixed by sending token with body parameter.

Try to send with Body parameters.

If this doesnt work then also check with changing middleware code

here is 1 line you need to change

public function handle($request, Closure $next)
{
    try{
        $user = JWTAuth::toUser($request->input('token'));
        // this will get token from body parameter
Subhash Chavda
  • 86
  • 1
  • 10