Laravelshoppingcart: Cart is not user safe

Created on 24 Jun 2016  路  8Comments  路  Source: Crinsane/LaravelShoppingcart

@Crinsane

Pertaining to a previous issue I raised #163, I disagree with the point that the Cart is user safe.

I have two sample users. I added an item using one of the users. Logged out. Then I logged in using the second user account. I can still view the items in the shopping cart added by the previous user. And I am pretty sure that both the accounts are different, as one is an account having admin privileges and the other is not.

I understand the concept of the usage of sessions here. If I log in the same user through a different browser, one browser shows the products in the cart, while other browser doesnt show it.

However, my concern is, if 2 users use the same browser to view the carts, then the contents should be separate for both right? Destroying the contents of the cart when the user logs out is a solution. But ideally, shouldnt be.

Most helpful comment

This is the way how i did it. I'm defining 2 events in EventServiceProvider
'auth.login' => [
'App\Listeners\RestoreCartOnLogin',
],
'auth.logout' => [
'App\Listeners\StoreCartOnLogout',
],
then 2 listeners. one with handle method Cart::store(auth()->id());, other Cart::restore(auth()->id());.Everythink work like charm.
You need Cart::instance(auth()->id()) like Crinsane tip.
Best regards.

All 8 comments

I'll look into this later. But for now the best way to make a cart (the session) specific for a given user, is use a custom instance. So for instance try something like: Cart::instance(auth()->id())->add(...);

The other option would indeed be to clear the cart session when the user logs out.

Yes it wiil by nice to look into this i have abs.same problem.

@Crinsane
Thanks for the tip.I ended up creating my own small shopping cart implementation using Eloquent databases instead of using Sessions. Persistence served my purpose. Your tip was working, but I didnt want any user to lose the items they added to their cart if they didnt check out immediately.

Ok but this with instance is working fine,i just have doubt thath this is right method.

This is the way how i did it. I'm defining 2 events in EventServiceProvider
'auth.login' => [
'App\Listeners\RestoreCartOnLogin',
],
'auth.logout' => [
'App\Listeners\StoreCartOnLogout',
],
then 2 listeners. one with handle method Cart::store(auth()->id());, other Cart::restore(auth()->id());.Everythink work like charm.
You need Cart::instance(auth()->id()) like Crinsane tip.
Best regards.

@Crinsane
Is there any method of the Cart class which can be used to save all the cart items in a database?
Best Regards
Vozo

Is this issue still open?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nasirkhan picture nasirkhan  路  8Comments

hatamiarash7 picture hatamiarash7  路  6Comments

muhammadatallah picture muhammadatallah  路  7Comments

rubelkhan447 picture rubelkhan447  路  6Comments

matbeard picture matbeard  路  7Comments