Passport: [4.0] Call to undefined method "withCookie"

Created on 31 Aug 2017  ·  7Comments  ·  Source: laravel/passport

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.

Most helpful comment

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

All 7 comments

To reproduce this bug:

  1. create fresh Laravel app with passport package
  2. scaffold auth (php artisan make:auth)
  3. register CreateFreshApiToken middleware
  4. create test route with response as: return response()->file(PATH_TO_SOME_FILE)
  5. login and go to test route -> you will get an error

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

parth-vora-7 picture parth-vora-7  ·  4Comments

cookiejarblush picture cookiejarblush  ·  4Comments

duccanh0022 picture duccanh0022  ·  3Comments

s4uron picture s4uron  ·  3Comments

seriousjelly picture seriousjelly  ·  3Comments