Brion Ballard
Brion Ballard

Reputation: 51

Removing A Cookie in Laravel

I'm having an issue removing a cookie in laravel. I set the cookie at login as 'sessionToken' and once the user logs out I flush the session, update sessionToken column to null, and forget the cookie. Yet, I can still see the cookie when I use "document.cookie" in the browser console.

How can I destroy that cookie & its value? I feel like this isn't normal behavior but I could be wrong.

public function logout(Request $request, User $_id)
{
    
    Auth::logout();

    $user = User::where('id', $request->$_id)->first();
    $user->sessionToken = null;
    $user->save();

    Session::flush();

    Cookie::queue(Cookie::forget('sessionToken'));

    return redirect()->route('cookie-login')->withCookie(Cookie::forget('sessionToken'));
    
}

Upvotes: 2

Views: 3511

Answers (2)

Louwki
Louwki

Reputation: 715

You can also do this, this removed the cookie from the browser.

Cookie::queue(Cookie::forget('name'));

Upvotes: 2

Brion Ballard
Brion Ballard

Reputation: 51

After much digging, I found a work around to remove the cookie. Setting the value to an empty string totally removes the cookie from the browser window.

$cookieValue = '';

Cookie::queue(Cookie::make('cookie', $cookieValue)) 

Upvotes: 0

Related Questions