Referrals and redirections
Brush comes with a powerful Brush.Referrals tool that is helpful in many ways.
Redirecting after login
Section titled “Redirecting after login”You may want to force users to be logged in to access a page. In such case, use Brush.Referrals.makeLoginRedirectUrl().
Redirect to the current page
Section titled “Redirect to the current page”Using Brush.Referrals.makeLoginRedirectUrl() generates a Shopify-ready URL according to this documentation.
It also correctly takes into account market-language directory for the current market avoiding the user to be redirected to a market that does not match the one he/she is coming from.
Example:
<div x-data="MyComponent()"><a :href="redirect">Please login</a></div>export default function () { Alpine.data("MyComponent", () => ({ redirect() { return Brush.Referrals.makeLoginRedirectUrl(); }, }));}Or, event simpler if your Alpine component does not need additional logic:
<a x-data :href="Brush.Referrals.makeLoginRedirectUrl()">Please login</a>Redirect to another page
Section titled “Redirect to another page”Just pass a relative path to the current domain.
Example:
Brush.Referrals.makeLoginRedirectUrl("policies/privacy-policy");Asynchronous referrer
Section titled “Asynchronous referrer”It is sometimes useful to save a URL that the user browsed and recover it later for some business logic.
Brush is shipped with Brush.Referrals.referrer, a tool that helps with such need.
Brush.Referrals.referrer.set(): saves the current URL for further use.Brush.Referrals.referrer.set('whatever'): saves “whatever” for further use.Brush.Referrals.referrer.get(): retrieves what was saved and flush it.Brush.Referrals.referrer.get(false): retrieves what was saved without flushing it so you can useBrush.Referrals.referrer.get()again to retrieve the same data.