Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Detect Admin/Shop Owner Logged into Store

Detect Admin/Shop Owner Logged into Store

hbaxton
Shopify Partner
4 0 1

I am writing an app, and I want the app to have an "admin" mode that detects when the Shop Owner, or if possible registered users designated by the Shop owner, are logged into the store.  When the app recognizes an "admin" is logged into the store, we will use this to show admin controls. 

Replies 2 (2)

jaystricks
Tourist
10 1 2

I'm also looking for guidance on this as well.

 

There is a User resource in the API, which has an attribute that indicates if the User is the store owner. However, the documentation says:

 

The User resource is available forprivate appsandcustom appsinstalled onShopify Plusstores. Contact Shopify Plus Support to enable this API resource for your store.

 

https://shopify.dev/api/admin-rest/2022-01/resources/user#top

 

I'm not sure if that means it's ONLY available for those apps, which seems strange given priority of Public apps. 


That said, I'm also trying to adjust our UX if the user is store admin, so any guidance from Shopify would be helpful.

darrynten
Shopify Partner
48 3 18

You can use content_for_header details to detect admin and more inside liquid.

 

The following example snippet will only load the app proxy script if the visitor is not:

 

(1) An Admin or Staff User

(2) Shopify Theme Designer

 

 

{% capture CFH %}{{ content_for_header }}{% endcapture %}
{% if CFH contains 'adminBar' or CFH contains 'designMode' %}
{% else %}
<script src="https://{{ request.host }}/apps/app-proxy-url"></script>
{% endif %}

 

 

All other visitors will trigger the app proxy.

 

It does this by capturing the content_for_header value and checking for the presence of code that Shopify only ever adds when an admin or staff member is logged in, or code that is appended by the theme designer.

 

If this code is present the result is an empty string, but if this code is not present (i.e. it's a normal visitor) the code will run and the app proxy script tag will get injected.

@darrynten