Go Pro Sign Up Login

Buy Now and unlock this series

$

49

.99

$

39

.99

yours forever

Become a Pro and unlock everything

$

24

.99

per month

Create A Higher Order Function Helper For Serverside Supabase Queries

Downloads

Become a pro to download code and videos

Comments

User avatar

Matt

about 1 month ago

As Shawn said, higher order functions are not supported in remix. I don't love my solution but it does work.

// withAuthRequired.js
import supabase from '~/utils/supabase';
import { getSession } from '~/utils/cookie';
import { redirect } from 'remix';

export default async (context) => {
  const session = await getSession(context.request.headers.get('Cookie'));
  const accessToken = session.get('accessToken');
  const { user } = await supabase.auth.api.getUser(accessToken);
  const result = {
    supabase,
    user,
    accessToken,
    redirect: null,
  };

  if (!user) {
    result.redirect = redirect('/login');
    return result;
  }

  await supabase.auth.setAuth(accessToken);

  return result;
};
// channels.jsx
export const loader = async ({ request }) => {
  const { supabase, redirect } = await withAuthRequired({ request });
  if (redirect) return redirect;
  const { data, error } = await supabase.from('channels').select('id, title');
  if (error) {
    console.log(error.message);
  }
  return {
    channels: data,
  };
};

Josh

about 1 month ago

Thanks for this.

Although I do wonder why this lesson is even in here when the whole premise of it doesn't work. The Remix docs are pretty clear about not being able to do what this lesson teaches.

Shawn

about 1 month ago [edited]

HOC functions do not work.

https://remix.run/docs/en/v1/guides/constraints#higher-order-functions

remove the wrapped function and call it directly in the loader or action...

Shawn

about 2 months ago

Same here... Please help...

HeaderParser.js:6 Uncaught ReferenceError: Buffer is not defined

Want to join the conversation?

Become a Pro member today!