I have created a custom middleware with the following code
class UuidMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
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));
}
}
Next I added this middleware in my kernel.php to the web middlewareGroups
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\UuidMiddleware::class,
],
'api' => [
'throttle:60,1',
],
];
Once I do this, I start getting errors on my login/registration/search etc. As soon as I remove this middleware the error disappears.
The Middleware logic is to check if a cookie named uuid is set. If its not set, it tries to set a uuid in the cookie for the user.
Can someone please help me debug on why this is happening.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire