Reputation: 1153
I am facing an issue where Laravel is not encrypting a Cookie that I am setting in a middleware. Due to this, when I try to retrieve the cookie value in another controller its value is coming blank. Below is my Middleware code
public function handle($request, Closure $next)
{
if($request->hasCookie('uuid'))
{
return $next($request);
}
else
{
$uuid = Uuid::generate();
if (Auth::check())
$user_id = Auth::user()->id;
else
$user_id = '';
Visitors::create([
'user_id' => $user_id,
'uuid' => $uuid
]);
$response = $next($request);
return $response->withCookie(cookie()->forever('uuid', $uuid));
}
}
I have created a UUID middleware to set a UUID parameter anytime someone comes to the site. I am using this middleware in my routes.
This is causing the UUID cookie to be set but it is not encrypted.
In another controller function, when I try to retrieve the value of the uuid cookie using
$uuid = Request::Cookie(uuid);
it is coming out blank.
(Note that I am using 2 middlewares for my route ('uuid', 'web'). If I change the sequence of middleware in my routes to ('web', 'uuid'), I start getting TokenExceptionError.)
Anyone faced a similar issue?
Upvotes: 1
Views: 1251
Reputation: 40909
It seems your uuid middleware is run before middleware that encrypts cookies. Middleware for your route group is defined as ['uuid', 'web'] and middleware that encrypts/decrypts cookies is in web group.
Add your middleware at the end of web group or change the order of middlewares in your route group to ['web', 'uuid'].
Upvotes: 0