mercredi 6 mars 2019

Laravel can't read a "recently" created cookie

I have a project who authenticate the user using cookies like token_ and refreshToken_, and a middleware who intercept my routes and verify if the user is logged or not.
In my middleware, when i need to renew the token_ I have the following code:

namespace App\Http\Middleware\VerifyAccessToken

$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);

It works apparently fine, but the problem is:
After the middleware intercep my route and renew the cookie, the request proced to his controller, but there, I can't access the cookie using $_COOKIE['token_'] and I get an error, but if I look in the chrome's inspector, the cookie is there and reloading the page (F5) I can access the cookie in controller
Have a method for me access the cookie in controller without need to go to the view before?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire