'Login not working after switching from database to cookie sessions in Laravel 8

We had the following code to logout and log back in a user, which works with database sessions in Laravel 8. But after switching to cookie sessions its not longer functioning. It now only logs the user out, not back in again.

    public function switchUser($user_id, Request $request)
    {
        $user = User::find($user_id);

        Auth::logout();
        Auth::login($user);

        return redirect('/');
    }

How can we fix this?

UPDATE

We updated SESSION_DRIVER from 'database' > 'cookie'



Solution 1:[1]

Flush session after logout

Auth::logout();
Session::flush();
Auth::login($user);

Remember to add Session

use Session;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rargueso