user1469734
user1469734

Reputation: 801

Laravel Request cookie doesn't show JS created Cookies

I'm using JS-Cookie for setting a Cookie via the front-end. Why can't I access it in my Laravel 5.8 application with: dd(request()->cookies)? The Cookie is visible with the name, but the value is null. But when I do try to get the Value of the Cookie via the "normal" way, I do get the value: dd($_COOKIE['the-cookie-value-i-want']);.

How can I access the value of the Cookie via the "Laravel-way"? Which is more secure.

Upvotes: 1

Views: 1308

Answers (2)

Brian Schrameck
Brian Schrameck

Reputation: 608

I know this is an old question, but you can disable encryption for specific cookies instead of disabling it for everything (which would break secure sessions).

Edit the \App\Http\Middleware\EncryptCookies class to include the following:

/**
 * The names of the cookies that should not be encrypted.
 *
 * @var array
 */
protected $except = [
    'my_frontend_cookie_name'
];

You can now access the cookie value via request()->cookie() or the Cookie::get() facade.

Upvotes: 3

Vipertecpro
Vipertecpro

Reputation: 3274

Only cookie created by laravel can handle by laravel.

Now here is the way you can achieve this

Go to : app > Http > Kernal.php : under $middlewareGroups array comment this

 'web' => [
            //\App\Http\Middleware\EncryptCookies::class,

And then try

Here is the demo

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>
    </head>
    <body>
        {{  Cookie::get('cookie1') }}
        {{  request()->cookie('cookie1') }}
        <script type="text/javascript">
            document.cookie = 'cookie1=test; path=/'
        </script>
    </body>
</html>

Upvotes: 0

Related Questions