Customer Status in JavaScript?

Explorer
127 0 15

Hi,

 

Wondering if anyone knows how to determine whether you're logged in or not, reliably with JavaScript? I've got a little ajax hack that basically checks the content type header of /account.js; it's application/javascript if you're logged in and text/html if you're not (due to a redirect), but obviously this isn't ideal. Is there a more official and stable way to do this, beyond ajaxing an application proxy that throws out an {% if customer %}1{% else %}0{% endif %}?

 

Thanks,

 

Adam

0 Likes
Highlighted
Shopify Staff
Shopify Staff
582 0 45

You could perhaps inspect the cookies and see if the field for a logged in user has been set.

1 Like
Explorer
127 0 15

I'm not sure why I didn't think of this. Thanks!

0 Likes
Shopify Partner
202 0 23

When you have access to the theme.liquid you could also set a javascript var:

{% if customer %}
<script type="text/javascript">
  var logged_in = true
</script>
{% endif %}

 

 

0 Likes
Explorer
127 0 15

Yeah; unfortunately, this is in a script tag. I could start uploading to the theme, I guess, but this would get messy; I'd have to detect all theme changes to ensure things were working properly, which I don't think you can webhook for.

0 Likes