Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

Cart session isn't maintained on logout with Laravel 5.3 #253

Open ChrisThompsonTLDR opened 7 years ago

ChrisThompsonTLDR commented 7 years ago

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');
            }
        });
ChrisThompsonTLDR commented 7 years ago

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);
    }
raylight75 commented 7 years ago

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.

wexford0709 commented 7 years ago

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

MacWorks commented 7 years ago

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.

sobhanatar commented 7 years ago

@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.