Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sessions with SPA application and API

We are using ASP CORE 2.x (API) with a SPA (ReactJS), I need to know what is the best strategy to track users sessions. The issue is mainly for unauthenticated users, let say we have a shopping cart. The cart is stored in the database, how do you keep the cart items of a guest user? When a guess leaves the site and comes back, we need to show the cart items.

I was planning to have a session generate by the server and saving it to the client side (in a cookie), but this means that I need to call a “ensure” session on from the client site to the server… and keeping track of the session in the db.

Any alternative or suggestions?

like image 929
Zulander Avatar asked Jun 25 '26 04:06

Zulander


2 Answers

You can persist the shopping cart items to localStorage, and then when the user comes back just rehydrate it. I would recommend redux-persist if you use redux.

like image 90
anerco Avatar answered Jun 26 '26 16:06

anerco


I would associate the cart of the guest to a generated guid stored with the cart (ID?}. Passing this through cookies, the next time the guest comes, if the cookie exists and refers to a cart, I reload it. You can set some timestamp to clean pending cart on a regular base from your DB.

like image 41
Cyril ANDRE Avatar answered Jun 26 '26 16:06

Cyril ANDRE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!