Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate an access token for anonymous customers. (To checkout without signing in) #358

Closed
chrispytoes opened this issue Nov 2, 2020 · 3 comments

Comments

@chrispytoes
Copy link

chrispytoes commented Nov 2, 2020

I am trying to figure out how to obtain a WooCommerce session token for the user if they don't sign in. I am using GatsbyJS for my front-end. I want customers to be able to checkout without signing in. I did some poking around in the GatsbyJS demo theme, and if I don't sign in, I can see the woocommerce-session header being sent on every GraphQL API request, but I can't figure out how that token is being obtained.

So how do I get one of these tokens without signing in so I can track cart items and checkout inside a WC anonymous customer session without signing in? The demo theme is clearly sending a JWT to the GraphQL API when I add items to my cart. So it's clearly getting a session from somewhere. However when I look at the source code, I only see the cart items being stored in local storage. I can't find any reference to the GraphQL API being used for the cart in the source code, but the demo itself is clearly making a request with a token to add items to my cart in a WooCommerce customer session, and I am not signed in. I deleted my cookies to be sure.

@jacobarriola
Copy link
Contributor

jacobarriola commented Nov 3, 2020

Hey @chrispytoes!

The WooCommerce session comes back in the headers of the response from the addToCart mutation. You can catch the incoming session token and store it in whatever way fits best for your app (localstorage, context, etc).

Apollo has some handy middleware that allows you to automatically listen for those headers (which come in as woocommerce-session).

It also has a handy middleware to send the session header on each request (ie adding an additional item to a cart, or getting cart items). That way you don't have to worry about authentication. As long as the session is in the header, WooCommerce will know what to do. 🙌🏽

@kidunot89 has a good example written here that I'm using in my app: #88

@kidunot89
Copy link
Member

@jacobarriola @chrispytoes query { customer { sessionToken } } should also work if a session has been started.

@chrispytoes
Copy link
Author

@jacobarriola @kidunot89 Thanks! I'm not too familiar with Apollo so that explains why I couldn't find the code actually interacting with the session header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants