Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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 %}
Solved! Go to the solution
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>
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>
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.
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025