Laravelshoppingcart: Cart session isn't maintained on logout with Laravel 5.3

Created on 18 Nov 2016  路  5Comments  路  Source: Crinsane/LaravelShoppingcart

Laravel 5.3 does a complete session flush when a user logs out.

The change to flush the entire session at logout was added in this commit.

    /**
     * Log the user out of the application.
     *
     * @param \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return redirect('/');
    }

I think it makes the destroy_on_logout config param irrelevant because Laravel kills the entire session.

        $this->app['events']->listen(Logout::class, function () {
            if ($this->app['config']->get('cart.destroy_on_logout')) {
                $this->app->make(SessionManager::class)->forget('cart');
            }
        });

Most helpful comment

Something like this might be the solution.

app/Http/Controllers/Auth/LoginController.php

public function logout(Request $request)
    {
        $cart = collect(session()->get('cart'));

        $destination = \Auth::logout();

        if (!config('cart.destroy_on_logout')) {
            $cart->each(function($rows, $identifier) {
                session()->put('cart.' . $identifier, $rows);
            });
        }

        return redirect()->to($destination);
    }

All 5 comments

Something like this might be the solution.

app/Http/Controllers/Auth/LoginController.php

public function logout(Request $request)
    {
        $cart = collect(session()->get('cart'));

        $destination = \Auth::logout();

        if (!config('cart.destroy_on_logout')) {
            $cart->each(function($rows, $identifier) {
                session()->put('cart.' . $identifier, $rows);
            });
        }

        return redirect()->to($destination);
    }

Same problem in Laravel 5.4, for now i comented this lines.
$request->session()->flush();
$request->session()->regenerate();
I've tried something like ChrisThompsonTLDR says but is not working for now.
If i find more elegant soluton i will write here.

Any update on this? I am having the same issue...

This is a problem for me too starting in 5.3. I rely on a session variable that get's set when the logout event is fired but the session gets flushed and that session variable is no longer available to me at response time.

@raylight75 I'm on 5.4 and solution @ChrisThompsonTLDR did give is working fine.
the complete answer is:

app/Http/Controllers/Auth/LoginController.php
public function logout(Request $request): JsonResponse
{
    try {
        $cart = collect($request->session()->get('cart'));

        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();

        if (!config('cart.destroy_on_logout')) {
            $cart->each(function($rows, $identifier) use ($request) {
                $request->session()->put('cart.' . $identifier, $rows);
            });
        }

        return response()->json([
            'status'   => true,
            'redirect' => $this->redirectTo()

        ], 200);

    } catch (Exception $e) {
        return response()->json([
            'status' => false, 'code' => $e->getCode()
        ], 422);
    }
}

```
notice that my project is multiple admin so I use guard() for logout and the function is overridden.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abdullahkaamil picture abdullahkaamil  路  3Comments

hatamiarash7 picture hatamiarash7  路  6Comments

gauravojha picture gauravojha  路  8Comments

matbeard picture matbeard  路  7Comments

morganchorlton3 picture morganchorlton3  路  3Comments