Laravel 5.1 access cookie generated in middleware before being redirected to view

Asked

Viewed 248 times

1

In my application I have an authentication system with token_ and refreshtoken_ and I have a middleware to verify that the user is logged in and to renew their token_ if it is expired.

After I renew the user’s cookies (in case the token is expired) it is redirected to the next page and on the home screen, for example, I try to access in a function o $COOKIE['token'] and it is not found and shows me an error, but when giving F5 I realize that the cookie has already been generated.

{ middleware: VerifyAccesToken }

$cookie_name = "token_";
$cookie_value = $obj->access_token;
$expires_in = $obj->expires_in;
$time = time() + $expires_in; // 3600 = 1 hora
$path = "/";
$domain = env('COOKIE_DOMAIN');
setcookie($cookie_name, $cookie_value, $time, $path, $domain, false, true);

$cookie_name = "refreshToken_";
$cookie_value = $obj->refresh_token;
setcookie($cookie_name, $cookie_value, $time + 3600, $path, $domain, false, true);

return $next($request);

My question is: is it possible for me to set the cookie and access it, before being redirected to a view? (access it in the controller as in this case)

1 answer

0

Hello, you can access it this way.

{{\Illuminate\Support\Facades\Cookie::get('refreshToken_')}} 

maybe for sure using

{{Cookie::get('refreshToken_')}} 

or

{{ cookie('refreshToken_') }}

take a test and share with us the result :)

  • 1

    Hello, thanks for the reply, I tried every way you said, and the only one who does not return NULL is the last, but it returns me that, the worthless cookie and the other data. NOTE: returns it to me even when I already have the set cookie and can access it using $_COOKIE['token_']

  • Now that I saw it, you are using the php setcookie and not the Laravel setcookie, using the Laravel setcookie you would create the cookie this way Cookie::queue($name, $value, $minutes); then the commands of my answer above will work. But you can continue using the native function without problem if you want... If the answer was useful don’t forget to mark it!

  • I’m trying to set the cookie with \Illuminate\Support\Facades\Cookie::queue('token_', $cookie_value, $time) and catch him with \Illuminate\Support\Facades\Cookie::get('token_') but still I get NULL. NOTE: even after updating the page and the cookie is already set for sure, I still get NULL

  • Standard is encrypting cookies an alternative is Laravel is Encrypting cookies, in the file App\Http\Middleware\EncryptCookies\: add the cookie name in except Example: protected $except = [ 'cookie_name'];

  • Putting protected $except = [ 'token_']; I can find the cookie (when it was already created and was already redirected to the page) using the \Cookie::get('token_') but returns me NULL if I try to access it directly in the controller after the cookie is set in middleware

  • Another thing I noticed, is that I’m not able to create a Cookie using Cookie::queue(), it just isn’t set

  • Actually "with you," the difference is that with setcookie() when I receive the error it generates the Cookie anyway, but with the Cookie::queue() it does not generate the cookie if I receive the error, but with either I can access the cookie before going to some view

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.