Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if current visitor is shop's admin?

I would like to create a product that will be available in Shopify's storefront but only accessible for the shop administrator. Is there a way to identify if the current user is an admin via liquid? Or is there any other solution for this. Thanks!

like image 237
vovafeldman Avatar asked Jul 04 '13 12:07

vovafeldman


2 Answers

If you're signed in as an admin, when rendering the {{ content_for_header }} include, it will contain some JavaScript to push the page content down to make room for the Shopify admin bar.

We can utilize capture to store the {{ content_for_header }} code as a liquid variable and then use the contains operator to check if admin_bar_iframe exists in the variable.

{% capture CFH %}{{ content_for_header  }}{% endcapture %}{{ CFH }}

{% if CFH contains 'admin_bar_iframe' %}
    {% assign admin = true %}
{% endif %}

{% if admin %}
    <!-- User is an admin -->
{% else %}
    <!-- User is not an admin -->
{% endif %}

Note: I've noticed that the Shopify admin bar doesn't populate at all times (I think its a bug). If your Shopify admin bar is not populating on your instance this will not work.

like image 173
kyle.stearns Avatar answered Nov 19 '22 14:11

kyle.stearns


Just figured out this method that works. (Basically the same thing as the old method, just another way around!)

Detecting logged in admin viewing site:

{% if content_for_header contains 'adminBarInjector' %}
   <script>
     console.log("You're a logged in admin viewing the site!");
   </script>
{% endif %}

Detecting admin in design mode:

{% if content_for_header contains 'designMode' %}
   <script>
     console.log("You're an admin in design mode!");
   </script>
{% endif %}
like image 26
Rachelle Wise Avatar answered Nov 19 '22 15:11

Rachelle Wise