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

7

9

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!

Vial answered 4/7, 2013 at 12:56 Comment(0)
M
16

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.

Medan answered 17/6, 2015 at 20:50 Comment(0)
W
7

Another approach would be to use Customer Accounts. Liquid provides a {{ customer }} object, which is only present when a user (customer) is logged in.

You can add a specific tag to an admin user and use liquid to verify if a tag is present:

{% if customer.tags contains "admin" %}

And of course you need to identify your product as 'admin-only', for example using tags:

{% if customer.tags contains "admin" and product.tags contains "admin" %}
  <!-- render product -->
{% else %}
  <!-- do nothing -->
{% endif %}
Weinshienk answered 5/7, 2013 at 7:21 Comment(2)
Can this approach be used when the customer login feature isn't activated?Vial
You can try it, but I assume the customer login feature needs to be activated ("Accounts are optional" or "Accounts are required").Weinshienk
P
7

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 %}
Propene answered 30/9, 2018 at 22:55 Comment(2)
This is a good solution. Circa 2021, you will want to look for 'previewBarInjector' rather than 'adminBarInjector'.Jilolo
it's now adminBar, designMode and/or previewBarMagistery
U
3

EDIT: This seems not to be working anymore since an update to Shopify.


I know this is late, but here is what I've used and it has worked correctly in my testing. This is adapted from the previous answers, but allows use for Customise Theme options, etc.

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

{% assign isAdmin = true %}

{% if CFH contains '"__st"' %}
    {% if CFH contains 'admin_bar_iframe' %}{% else %}
        {% assign isAdmin = false %}
    {% endif %}
{% endif %}
Uranometry answered 19/4, 2017 at 7:57 Comment(0)
P
2

This is a more complete version of that provided by kyle.stearns above (I can't comment on it because new). One of the main instances where admin bar doesn't load is when previewing themes. Here is the amended code which I've used (updated June 2018 - as Shopify edited the way we preview themes):

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

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

{% if admin %}
<!-- User is an admin -->
<script>
  alert ("do some work");
</script>
{% else %}
<!-- User is not an admin -->
<script>
  alert ("please buy some stuff");
</script>
{% endif %}

If you're using Plus then you also have access to the User via api.

Provencal answered 13/2, 2017 at 11:32 Comment(0)
M
2

I know this is an old question, but it still shows on top in google searches.

There's now a much better way.

  1. Using liquid:
{% if request.design_mode %}
  <!-- This will only render in the theme editor -->
{% endif %}

  1. Using javascript:
if (Shopify.designMode) {
  // This will only render in the theme editor
}

Source: shopify.dev

Merete answered 12/7, 2022 at 9:44 Comment(1)
this is an answer to a different question. it does not answer how to tell if the current user is an admin.Magistery
A
0

Currently there isn't. You could perhaps try inspecting cookies and stuff to see if there's some identifying information that would let you know if the user is an admin, but it would be fragile.

This would also require rendering the items, but hiding them via CSS. Then you'd show them using JS after you've run your checks.

As stated, this would probably be really fragile.

Assuan answered 4/7, 2013 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.