1
votes

I have a laravel application that I have recently installed laravel passport with. This application makes a series of internal api calls to retrieve data. With laravel passport whenever I make these internal api calls it gives back an unauthenticated error message.

I have added the 'auth:api' middleware to my api routes and want to be able to access them internally. Do I need to send a bearer token with the request to allow the request through. Also how would I generate this token?

Here is an example of the code with call to make internal api calls.

public function postToApi($url, $data) {
    $req = Request::create($url, 'POST', $data);
    try {
        $res = app()->handle($req);
    } catch (Exception $e) {
        return "not found";
    }
}

Here is an example of how the routes look

Route::group(['middleware' => 'auth:api', 'prefix' => 'api', 'namespace' => 'Modules\Forum\Http\Controllers'], function(){
Route::get('reply/{id}', 'ReplyApiController@show' );
Route::get('thread/{id}', 'ThreadApiController@show' );});
1
There's this whole bit about consuming your own api with passport in the docs. laravel.com/docs/5.7/…John Halsey

1 Answers

1
votes

The first solution is adding this to .htaccess of root folder (not only inside the public folder)

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

this second solution into your Kernel.php middleware groups.

\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,

the third solution

/**
/* Indicates if cookies should be serialized.
/*
/* @var bool
 */
protected static $serialize = true;

in Http/Middleware/EncryptCookies.php fixed it for me.