Grigory Volkov
Grigory Volkov

Reputation: 482

Can't take Laravel cookie that's been set

I have a checkbox in my login form and if it is checked I want to save user's email and password in a cookie so then he'll be automatically logged in. This is the part of my code that checks if the checkbox is checked and sets the cookie.

if ($remember == 'on') {

                    $array = array(

                        "email" => $email,
                        "password" => $password

                    );

                    $time = time() + (10 * 365 * 24 * 60 * 60);

                    Cookie::queue('user', serialize($array), $time);

                }

But when I try to get the cookie and print it out it prints "false".

$cookie = Cookie::get('user');

dd(unserialize($cookie));

How can I get the cookie? It shows the cookie that's been set in inspect. What's the best way to check if the cookie is set so i'll redirect the user to profile view straight away? Do I have to write a middleware for it?

Upvotes: 0

Views: 91

Answers (1)

Mehdi
Mehdi

Reputation: 756

The problem is your dd function call.

Cookies will store in user's browser when he retrieve the response with your Cookies attached (set-cookie headers), and you can read them in the next request that he will send to you.

when you call dd, it breaks the response chain and removes the set-cookie headers.

just remove the dd and in another route write:

dd(unserialize(\Request::cookie('user')));

Upvotes: 1

Related Questions