Hello,
Laravel: 5.5
Passport: 4.0
Problem:
(1/1) FatalThrowableErrorCall to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::withCookie()
--
in CreateFreshApiToken.php (line 51)
I switch from 3.0 to 4.0 and now I have the problem.
I use in my ImageController directly BinaryFileResponse (resp. Intervention/image) but this does not have the method "withCookie".
This change is the problem: https://github.com/laravel/passport/commit/df5ddb28318a6ffac2cb0c96fc30e045afa2f9b9#diff-21f5bd46f0aa959bdc94060671ea5b18L92
I think we should add $response instanceof Response again.
To reproduce this bug:
php artisan make:auth)CreateFreshApiToken middlewarereturn response()->file(PATH_TO_SOME_FILE)The solution is what xeno010 suggested.
I am also experiencing this issue, is there already a known fix without downgrading or editing the source code?
Hit this issue as well.
A workaround could be to make a new middleware group for file downloads and make sure that \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, is not included , but that seems like it would just add extra complexity.
From what I can see the idea behind the PR counters the ability to have support for custom responses.
https://github.com/laravel/passport/pull/474
The PR brings a good idea in, not sure the best way to get around it, trying to factor in every kind of response seems like a slippery slope. Though that is just my thoughts.
A new middleware group is to complicated.
Create a new middleware extend the CreateFreshApiToken class and override the
responseShouldReceiveFreshToken methode
See:
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Response;
class CreateFreshApiToken extends \Laravel\Passport\Http\Middleware\CreateFreshApiToken
{
/**
* Determine if the response should receive a fresh token.
*
* @param \Illuminate\Http\Response $response
* @return bool
*/
protected function responseShouldReceiveFreshToken($response)
{
return $response instanceof Response && ! $this->alreadyContainsToken($response);
}
}
The Kernel Class
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
// ...
// \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, // <- old
\App\Http\Middleware\CreateFreshApiToken::class, // <- new
// ...
],
This works very well
I also think the mentioned PR causes problem because when using Response::download you will get the error @themsaid can you look closer at this?
Using laravel-passport "v4.0.2" still gives me "Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::withCookie()" when I try to download a file... However the workaround suggested by @xeno010 see here works fine
@taylorotwell Should we wait for the next minor update in order to have this issue fixed?
Thank you
I've had the exact same problem. @xeno010 's workaround worked for me too. Thanks a lot.
Most helpful comment
A new middleware group is to complicated.
Create a new middleware extend the
CreateFreshApiTokenclass and override theresponseShouldReceiveFreshTokenmethodeSee:
The Kernel Class
This works very well