Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 3.71 KB

File metadata and controls

69 lines (50 loc) · 3.71 KB

🏡 Home

Create Supabase project Create Remix application Query Supabase data with Remix Loaders Generate TypeScript types from Supabase CLI Implement Supabase Auth using GitHub Restrict access with RLS policies Automatically set session cookie with Supabase Auth Helpers Call active Loaders on Supabase Auth state change Mutate Supabase data with Remix Actions Subscribe to database changes with Supabase Realtime Deploy Remix app to Vercel

Call active Loaders on Supabase Auth state change

📹 Video

The OAuth flow is asynchronous. This means we get redirected to the landing page before GitHub and Supabase have decided that you can be trusted! 👍

Since our Loader functions get called when we first load the page they make a request to Supabase before receiving a valid access token. This causes RLS to deny the request to select data.

Once our session has been correctly set, Supabase is happy but we aren't telling Remix to fetch this data again. In this lesson, we look at using the onAuthStateChange hook from Supabase to submit a post request to an empty action. Anytime an Action function completes, Remix recalls any active loaders to keep data in sync with mutations.

Therefore, any time the user's session is updated - the auth flow has completed for either signing in or out - Remix will automatically call all loader functions that are active on the current route (this could be many with nested routing), fetching fresh data from Supabase with a valid access token.

Code Snippets

onAuthStateChange listener

useEffect(() => {
  const {
    data: { subscription },
  } = supabase.auth.onAuthStateChange((event, session) => {
    if (session?.access_token !== serverAccessToken) {
      fetcher.submit(null, {
        method: "post",
        action: "/handle-supabase-session",
      });
    }
  });

  return () => {
    subscription.unsubscribe();
  };
}, [serverAccessToken, supabase, fetcher]);

Empty action

export const action = () => {
  return null;
};

Resources


👉 Next lesson


Enjoying the course? Follow Jon Meyers on Twitter and subscribe to the YouTube channel.