Current best way to get logged in customer id

2 words of caution for anyone using liquid to grab the customer ID:

  1. 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.

  1. 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.

1 Like