I’m looking for some advice on the best way to get the current logged in customer id. Right now I rely on app proxy pages, but I’m not really happy with that solution. I’ve seen people use some Javascript supplied by Shopify to check a variable:
__st.cid
But it doesn’t seem like it’s intended to be used by developers and could change without notice. What solutions are people finding the most success with? Are there any plans to have better support for this in the near future?
Can I also use this in a HTTP response from a proxy URL if the Content-Type is set to ‘application/liquid’? My response relies on the customer that is currently logged in.
Hi, You can get the customerid from the window object but the specification is always changing and different customers may be using different versions.
My question is how to test this functionality in dev store? So far we are not able to make our dev store accept shop customer login, or I’m missing something?
2 words of caution for anyone using liquid to grab the customer ID:
Be wary of caching.
If the store is using some aggressive (especially CDN-level caching) then it may cache pages with the wrong customer ID.
Properly configured caching shouldn’t have this issue, but all know that the rush to “make your site faster” don’t always do things properly.
Using __st.cid is a better alternative for this reason than the liquid variable as it is not cacheable via CDN.
Frontend customer ID isn’t secure.
Never trust the customer ID you get from liquid or JavaScript.
You can use the [sha256 liquid filter](http://if%20(typeof __st !== ‘undefined’ && __st.cid) { opts.logger.log(Set currently logged in customer as ${ __st.cid }); opts.app.setCustomerId(__st.cid); }) with a secret code (like your app’s secret yet) to securely identify the customer instead.
Just clarification - the only place you should have customer details in the templates is for pages that require a login.
DO NOT include {{ customer.email }} in places like the theme.liquid or product pages or it will be cached by Shopify’s CDNs and may leak to other customers.