vendure-ecommerce / storefront-remix-starter

A storefront starter kit for Vendure built with Remix
https://remix-storefront.vendure.io
188 stars 105 forks source link

How to make site usable only for logged in users #62

Closed xdabuss closed 9 months ago

xdabuss commented 9 months ago

Is there a clever way to "lock" the site so that only logged in users can see anything besides the sign-in page? I've been adding this in all the loaders of each route, but not sure if there's a cleaner/ more robust way?

const { activeCustomer } = await getActiveCustomerDetails({ request });
  if (!activeCustomer) {
    return redirect('/sign-in');
  }
oidt commented 9 months ago

I am not really an export on this topic, but how i tried this:

root.tsx in the loader function: const url = new URL(request.url); if(url.pathname !== '/sign-in') { return redirect('/sign-in'); }

in the App() function if(!loaderData?.activeCustomer?.activeCustomer) { return (); //no header or footer } else { return (); //show all }

parkashay commented 9 months ago

You can just check for activeCustomer in the root loader and redirect to /sign-in if it doesn't exist.

xdabuss commented 9 months ago

That makes sense and seems to do the trick, thanks!