Mahmood Kohansal
Mahmood Kohansal

Reputation: 1041

Laravel - Delete Cookie in Middleware

I have a middleware in my project that call in every request. It will check if Request has a specific cookie, then delete another cookie. But it seems Cookies are not forgotten or set in Laravel until return in the response. something like return response('view')->withCookie($cookie); that is not possible in middlewares.

Also I tried Cookie::queue(Cookie::forget('myCookie')); nothing happened and cookie is shown in my browser.

This is my middleware handle method:

public function handle(Request $request, Closure $next)
{
    if (! $request->cookie('clear_token')) {
        cookie()->forget('access_token');                 # not worked
        Cookie::queue(Cookie::forget('access_token'));    # not worked
    }

    return $next($request);
}

Upvotes: 1

Views: 1114

Answers (1)

Rolf
Rolf

Reputation: 638

You can change the response in middleware too:

https://laravel.com/docs/5.0/middleware

<?php namespace App\Http\Middleware;

class AfterMiddleware implements Middleware {

    public function handle(\Illuminate\Http\Request $request, \Closure $next)
    {
        $response = $next($request);

        // Forget cookie

        return $response;
    }
}

Upvotes: 0

Related Questions