Skip to content

Referrals and redirections

Frontend

Brush comes with a powerful Brush.Referrals tool that is helpful in many ways.

You may want to force users to be logged in to access a page. In such case, use Brush.Referrals.makeLoginRedirectUrl().

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:

some-liquid-file.liquid
<div x-data="MyComponent()"><a :href="redirect">Please login</a></div>
some-component.ts
export default function () {
Alpine.data("MyComponent", () => ({
redirect() {
return Brush.Referrals.makeLoginRedirectUrl();
},
}));
}

Or, event simpler if your Alpine component does not need additional logic:

some-liquid-file.liquid
<a x-data :href="Brush.Referrals.makeLoginRedirectUrl()">Please login</a>

Just pass a relative path to the current domain.

Example:

some-component.ts
Brush.Referrals.makeLoginRedirectUrl("policies/privacy-policy");

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 use Brush.Referrals.referrer.get() again to retrieve the same data.