Laravel 5.4 - Cookie Queue
Asked Answered
L

2

6

I'm using Laravel 5.4 and I wrote something like:

     Cookie::queue(
        'refresh_token',
        $data->refresh_token,
        864000, // 10 days
        null,
        null,
        false,
        true // HttpOnly
    );

    return response('hello world');

The returned response doesn't contain the refresh_token cookie while return response('hello world')->withCookie(...) does.

The Laravel 5.4 documentation doesn't anymore state queueing cookie as 5.0 doc does. Does it mean that the functionality has been removed in version 5.4 or did I make a mistake in my code?

For sake of completeness, I'm using Dingo API package and the response is crafted it.

Thank you for your help.

Larios answered 19/7, 2017 at 4:26 Comment(1)
Be careful, the "10 days" comment is not good. The value is passed in minutes and not in seconds. To expire in 10 days you need 10*24*60 = 14400Juline
L
44

I found that:

Cookie queuing is not enabled for api requests, this is the reason why it didn't work.

I had to add in the middleware section of the appropriate file:

protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

        //added below line at end of the array
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    ];

Open file App/Http/Kernel.php add the line \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, in protected $middleware array as displayed in above code snippet and test again it should work now.

Larios answered 22/7, 2017 at 5:50 Comment(2)
You saved my life :)Ar
Spectacular answer!Mislay
A
2

In case anyone fond their way here by Google, one way for cookie inclusion to silently fail is if you're explicitly defining your domain variable in its creation, and forgot to remove the "http://" from the beginning of it first. That's not the case with OP, but it was what brought me here. ;)

Arleanarlee answered 26/4, 2018 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.