How to ensure customer login for my discount app?

I have created an app for discounts that will apply to the logged-in user. I have followed a Shopify document to create a discount app. However, I am not able to get the customer logged in to the store or not.

Hi @vishal_chandola ,

To determine if a customer is logged in to your Shopify store from within an app, you typically need to use Shopify’s APIs. Since you’ve created a discount app, you’ll need to integrate with Shopify’s storefront or admin API to check the customer’s login status. Here’s a general approach to achieve this:

  1. Use Shopify Storefront API:

    • The Storefront API can be used to create client-side applications that interact with the customer’s data in Shopify.
    • To check if a customer is logged in, you can query the customer Access Token object. This token is provided when a customer successfully logs in.
  2. Use Shopify Admin API:

    • The Admin API is used for back-end interactions with your Shopify store.
    • While it doesn’t directly tell you if a customer is logged in, you can use it to manage customer data and create discount rules.
  3. Create a Customer Login Status Check:

    • Implement a system that checks for a valid customer Access Token. If a token exists and is valid, it indicates that the customer is logged in.
    • You can create an AJAX call that hits an endpoint in your app, which then makes a query to the Storefront API to validate the customer’s login status.
  4. Consider Using Cookies or Session Variables:

    • Depending on your app’s architecture and how it interacts with the Shopify storefront, you might be able to use cookies or session variables to track login status.
    • Be mindful of privacy and data security regulations when using cookies or storing customer data.
  5. Frontend Integration:

    • If your app has a frontend component running on the Shopify store, you can use JavaScript to check for elements that are only present or different when a user is logged in.
    • This method is less reliable and more of a workaround, as it depends on the store’s theme and any changes could affect the detection.
  6. Review Shopify’s API Documentation:

    • Regularly review Shopify’s API documentation for any updates or new features that could simplify this process.
    • The Shopify community forums and developer resources can also be helpful for finding solutions to common problems.
  7. Testing and Validation:

    • Thoroughly test your implementation in different scenarios to ensure it accurately detects a customer’s login status.
  8. Consider Edge Cases:

    • Account for situations where a customer might log out in another tab or their token expires. Your app should handle these cases gracefully.

If you’re still facing issues, it might be helpful to share specific details about the problem, such as the API calls you’re making, the responses you’re receiving, and any error messages. This information could provide more insights into the issue and how to resolve it. Remember, handling customer data securely and respecting privacy is paramount, so ensure your solution aligns with best practices and legal requirements.

This may be too late but in case it can help someone else, the way you apply discounts to a logged-in user is by adding the buyerIdentity query in run.graphql like so:

query RunInput {
  cart {
    buyerIdentity {
      isAuthenticated
    }
    lines {
      ...
    }
  }
}

Then, in run.js, check for the value of isAuthenticated:

if (input.cart.buyerIdentity?.isAuthenticated)