Basket empty after login

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
snicto
Posts: 37
Joined: 12 Oct 2022, 14:00

Basket empty after login

Post by snicto » 01 Feb 2023, 15:28

Version: 2022.10

I got the pages to fully show up, function, etc. However, I am having a small issue with retaining basket data. I cannot figure out why when the user logs in the basket and data is lost. Below you can see my "AuthenticatedSessionController.php":

Code: Select all

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Aimeos\Shop\Facades\Shop;
use Illuminate\Support\Facades\Response;

class AuthenticatedSessionController extends Controller
{
    /**
     * Display the login view.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {

        $params = ['page' => 'page-index'];

        foreach( app( 'config' )->get( 'shop.page.cms', ['cms/page', 'catalog/tree', 'basket/mini'] ) as $name )
		{
			$params['aiheader'][$name] = Shop::get( $name )->header();
			$params['aibody'][$name] = Shop::get( $name )->body();
		}

		return Response::view( Shop::template( 'account.login' ), $params )
			->header( 'Cache-Control', 'no-store, private' );

    }

    /**
     * Handle an incoming authentication request.
     *
     * @param  \App\Http\Requests\Auth\LoginRequest  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(LoginRequest $request)
    {
        $request->authenticate();
        
        $request->session()->regenerate();

        return redirect()->intended(RouteServiceProvider::home());
    }

    /**
     * Destroy an authenticated session.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Request $request)
    {
        Auth::guard('web')->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return redirect(airoute( 'aimeos_home' ));
    }
}
Basically, if a user adds items to the basket, and logs in, the basket is emptied. Which is not the desired outcome. Is there something I am missing as a requirement?

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Auth pages using Aimeos views

Post by aimeos » 02 Feb 2023, 13:50

This line may create a new session:

Code: Select all

$request->session()->regenerate();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

snicto
Posts: 37
Joined: 12 Oct 2022, 14:00

Re: Auth pages using Aimeos views

Post by snicto » 02 Feb 2023, 16:49

It is the default implementation of the store handler.

snicto
Posts: 37
Joined: 12 Oct 2022, 14:00

Re: Basket empty after login

Post by snicto » 07 Feb 2023, 20:16

I was testing this on a clean install of the latest version of the stand-alone Laravel version. And the same is happening there. If you have anything in the basket and log in, the basket contents are lost and it is empty. Is it meant to be that way? Kinda illogical that person would lose the basket upon logging in.

Also additional while I was debugging I came across a couple of issues with the mini-basket not updating properly:
1. Open a new instance of a browser
2. Go to the product and add to the basket, once pops up, hit back. [mini-basket shows 1]
3. Click aimeos logo (goes to the first page), mini-basket shows 0
Works on https://laravel.demo.aimeos.org

1. Open a new instance of a browser
2. Go to the product and add to the basket, once pops up, hit back.
3. Switch to a different language, the basket becomes empty
Works on https://laravel.demo.aimeos.org

Am I missing some configuration somewhere to enable this logic? Or could I be pointed in the right direction to resolve this?

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Basket empty after login

Post by aimeos » 09 Feb 2023, 09:13

snicto wrote: 07 Feb 2023, 20:16 I was testing this on a clean install of the latest version of the stand-alone Laravel version. And the same is happening there. If you have anything in the basket and log in, the basket contents are lost and it is empty. Is it meant to be that way? Kinda illogical that person would lose the basket upon logging in.
It's not the desired behavior and seems to be a side effect because Laravel changes the session ID after login (but keeps the session data). Aimeos uses the session ID to store the serialized basket in the database since 2022.10 and if it changes, the basket isn't found any more. We've changed the code in the Aimeos Laravel package slightly to store the original session ID in the Laravel session so it's stable as long as the session is available. Can you please test the new version using:

Code: Select all

composer req aimeos/aimeos-laravel:2022.10.x-dev
snicto wrote: 07 Feb 2023, 20:16 Also additional while I was debugging I came across a couple of issues with the mini-basket not updating properly:
1. Open a new instance of a browser
2. Go to the product and add to the basket, once pops up, hit back. [mini-basket shows 1]
3. Click aimeos logo (goes to the first page), mini-basket shows 0
Works on https://laravel.demo.aimeos.org
This is due to browser caching because the page can be cached up to 30 sec by the browser. Thus, it will show the page from the browser cache and don't ask the server:
https://github.com/aimeos/aimeos-larave ... er.php#L91

We want to avoid loading each page from the server only for that reason because when leveraging browser caching, you handle 10 times more requests than without. A possible solution to mitigate the problem would be to store the current number of products in the basket within the browser and update it with a few lines of Javascript.
snicto wrote: 07 Feb 2023, 20:16 1. Open a new instance of a browser
2. Go to the product and add to the basket, once pops up, hit back.
3. Switch to a different language, the basket becomes empty
Works on https://laravel.demo.aimeos.org
We can't reproduce the behavior in the Laravel demo every time and we have to investigate more. The problem seems to be that products are not always migrated to the new basket for the new language even if the product is available.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

snicto
Posts: 37
Joined: 12 Oct 2022, 14:00

Re: Basket empty after login

Post by snicto » 09 Feb 2023, 14:41

aimeos wrote: 09 Feb 2023, 09:13
snicto wrote: 07 Feb 2023, 20:16 I was testing this on a clean install of the latest version of the stand-alone Laravel version. And the same is happening there. If you have anything in the basket and log in, the basket contents are lost and it is empty. Is it meant to be that way? Kinda illogical that person would lose the basket upon logging in.
It's not the desired behavior and seems to be a side effect because Laravel changes the session ID after login (but keeps the session data). Aimeos uses the session ID to store the serialized basket in the database since 2022.10 and if it changes, the basket isn't found any more. We've changed the code in the Aimeos Laravel package slightly to store the original session ID in the Laravel session so it's stable as long as the session is available. Can you please test the new version using:

Code: Select all

composer req aimeos/aimeos-laravel:2022.10.x-dev
Yes, this has resolved the losing basket on login and registration. Thanks!

User avatar
VadimSaratov
Posts: 8
Joined: 09 May 2022, 15:27

Re: Basket empty after login

Post by VadimSaratov » 29 Jun 2023, 08:32

when you try to log in in another browser or on another device, the basket will be empty. I think this is expected behavior as the session ID and cookies are different, is there any update on this in the aimeos versions? Or I need to change this behavior by overwriting the getSession/setSession() methods in the order base manager

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Basket empty after login

Post by aimeos » 30 Jun 2023, 07:46

In 2023.04, baskets are stored in the database by default now:
https://github.com/aimeos/aimeos-core/b ... #L125-L147
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kopz
Posts: 8
Joined: 18 Jan 2024, 14:11

Re: Basket empty after login

Post by kopz » 21 Feb 2024, 13:55

when you try to log in in another browser or on another device, the basket will be empty. I think this is expected behavior as the session ID and cookies are different, is there any update on this in the aimeos versions? Or I need to change this behavior by overwriting the getSession/setSession() methods in the order base manager
I have the same problem. Any tips on modifying the order manager to solve this?

User avatar
aimeos
Administrator
Posts: 7871
Joined: 01 Jan 1970, 00:00

Re: Basket empty after login

Post by aimeos » 23 Feb 2024, 09:57

Overwrite the getSession()/setSession () methods and use the customer ID if available instead of the token:
- https://github.com/aimeos/aimeos-core/b ... hp#L69-L75
- https://github.com/aimeos/aimeos-core/b ... #L128-L134

In 2024.x+ it's in the Session trait but you have to overwrite the order manager class the same way:
https://github.com/aimeos/aimeos-core/b ... ession.php

You get the customer (ID) using "$context->user()".
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply