Guest Session in Back-End

Sourav SarkarSourav Sarkar
2 min read

I learned how to work with Guest Session in back-end with node.js and mongoose. Suppose if you want to create a E-commerce like **Amazon.**Then you want to implement a functionality, example: When a user see a product and like it and want to buy it in future so user will make it for add to cart but in your case this user have no account on your platform and also user don’t want to create a account for now but using session you can do it and when user create a account on this same device then which products the user added to cart will show on user’s cart after creating a account and successful login and without creating a account these products will be available for a time that will you will give the user.

Steps:

  1. When user first time visit your website, you will create a session for this user and same device.

  2. You will stored this session id on your database like MongoDB.

  3. Give a expiry time (28 Days).

  4. Make route or api on back-end when user add a product to cart this product will be save in your database for show to user’s cart component.

  5. If session time is expired then delete the user’s data from your database using MongoDB auto deletion documents.

  6. If session time isn’t expired and user create a account then get session id from cookie.

  7. Search the session id from database and create a user with added products.

  8. After successful login user can see all of these products that he had added to cart before creating a account.

0
Subscribe to my newsletter

Read articles from Sourav Sarkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sourav Sarkar
Sourav Sarkar