Members Only Page for logged in customers only

Solved

Members Only Page for logged in customers only

skyeabd
Visitor
2 0 0

I have a page that I would like restricted to only show when customers are logged in. I want to redirect customers to login if they aren't logged in. Currently I have tried this code and what is happening is when the customer logs in and goes back to view the page, it opens customers account/orders page and not the original page that is locked. It seems to continuously redirect to customer accounts instead of opening the page. (The page is named "page.bart-s-army.json")

 

This code is in the theme.liquid file.

 

<html lang="{{ shop.locale }}">
  
  {% if template contains "army" %}
    {% unless customer %}
        {% assign send_to_login = true %}
    {% endunless %} 
  {% endif %}
  
  {% if send_to_login and request.path != "/challenge" %}
    <meta content="0; url=/account/login?checkout_url={{ request.path }}" http-equiv="refresh" />
  {% else %}

  <head>
    
    <!-- the <head> section of the file should go here -->

  </head>

  <body>

    <!-- the <body> section of the file should go here -->

  </body>

  {% endif %}
Accepted Solution (1)

oscprofessional
Shopify Partner
16343 2438 3177

This is an accepted solution.

Hi @skyeabd ,

You're on the right track, but the issue comes from the way Shopify handles redirects after login. The checkout_url parameter in account/login?checkout_url={{ request.path }} doesn't automatically redirect users to the intended page after login. Instead, Shopify defaults to the customer account page.

Fix: Store the Intended Page Before Redirecting

You need to store the original URL in a query parameter and then handle redirection after login.

Fixed Code

Modify your theme.liquid file:

 

<html lang="{{ shop.locale }}">

  {% if template contains "army" %}
    {% unless customer %}
        <script>
          var returnUrl = encodeURIComponent(window.location.pathname);
          window.location.href = "/account/login?return_url=" + returnUrl;
        </script>
    {% endunless %}
  {% endif %}

  <head>
    <!-- the <head> section of the file should go here -->
  </head>

  <body>
    <!-- the <body> section of the file should go here -->
  </body>

</html>

 

 

Hire us | Pass Core Web Vital | B2B Wholesale Experts | Claim Your Free Website Review |
Connect with Us: WhatsApp | Skype: oscprofessionals-87 | Email: pallavi@oscprofessionals.com |
Custom Shopify SolutionsPrivate Apps, Theme Customization & SEO | Digital Marketing |
OSCP Apps: Discount Suite | Wholesale App | Bundle & Upsell | Shipping Discount | and more...

View solution in original post

Replies 2 (2)

oscprofessional
Shopify Partner
16343 2438 3177

This is an accepted solution.

Hi @skyeabd ,

You're on the right track, but the issue comes from the way Shopify handles redirects after login. The checkout_url parameter in account/login?checkout_url={{ request.path }} doesn't automatically redirect users to the intended page after login. Instead, Shopify defaults to the customer account page.

Fix: Store the Intended Page Before Redirecting

You need to store the original URL in a query parameter and then handle redirection after login.

Fixed Code

Modify your theme.liquid file:

 

<html lang="{{ shop.locale }}">

  {% if template contains "army" %}
    {% unless customer %}
        <script>
          var returnUrl = encodeURIComponent(window.location.pathname);
          window.location.href = "/account/login?return_url=" + returnUrl;
        </script>
    {% endunless %}
  {% endif %}

  <head>
    <!-- the <head> section of the file should go here -->
  </head>

  <body>
    <!-- the <body> section of the file should go here -->
  </body>

</html>

 

 

Hire us | Pass Core Web Vital | B2B Wholesale Experts | Claim Your Free Website Review |
Connect with Us: WhatsApp | Skype: oscprofessionals-87 | Email: pallavi@oscprofessionals.com |
Custom Shopify SolutionsPrivate Apps, Theme Customization & SEO | Digital Marketing |
OSCP Apps: Discount Suite | Wholesale App | Bundle & Upsell | Shipping Discount | and more...
skyeabd
Visitor
2 0 0

Thank you so much for the help, I was struggling with what was going wrong for so long and this helped immensely. I am also wondering if there's a way to exclude the login redirect when I am wanting to edit the page as I am running into the issue of not being able to edit in Shopify theme editor as the url tries to direct me to login and then breaks as its in theme editor. Is there a way to allow me access when editing the store page? I have tried logging in inside the editor but it does not work.