Guest cart issue using "bagisto/rest-api": "^2.1"
-
Hello Bagisto Team,
I am using rest-api version 2.1. I am not able to add the product to the cart.
Endpoint: {{baseUrl}}/api/v1/customer/cart/add/476 Request: { "is_buy_now": 0, "product_id": 476, "quantity": 1 } Response: { "message": "Unauthenticated." }
Can you please help me with this?
Thank you
-
Hello @marshalbagisto ,
As I can see in your request and response data, it clearly shows you are not authenticated by the user. Please login first with any user; after that, try to hit
Add to Cart
API. It will work for only logged-in customers.Please try doing this, and if you get an issue, let us know.
-
@amit-webkul Hi Amit!
Yes, after user login it is working already. The issue is. I want to prompt the user to log-in while checkout and do them regular cart operations without log-in. Do you think this is possible? If yes, can you please guide me?
Thank you
-
If you want to add user login to the checkout section, and before that, the cart should work for a guest also, so you need to override the cart and checkout API and customize as per your need.
Like first, you need to provide access to the cart for guest users. After doing this, you will need to manage the checkout API as well.
-
The answers from @amit-webkul are not what you are looking for probably.
In rest-api we don't get any routes for guests, everything is based on JWT for already logged in users.
If you want to do operations for guests you need to use XSRF tokens as authentication.
in order to do that, you need to call api/customer/cart GET (not api/v1/customer/cart), this will SET xsrf-token cookie for your domain.
after that, on adding to cart and every other operation you need to include that xsrf token
credentials: 'include', 'Cookie': `XSRF-TOKEN=${useCookie('XSRF-TOKEN')}; oronegro_session=${useCookie('oronegro_session')}`, 'X-XSRF-TOKEN': useCookie('XSRF-TOKEN')
-
@Peppek1993 Thank you. I'll try and update here the progress.