GithubHelp home page GithubHelp logo

Comments (2)

Xavier-IV avatar Xavier-IV commented on September 26, 2024

Hi, I would like to add on this.

In "Step 5: Set auth token with Supabase", it is mentioned to use supabase.auth.setAuth(token).

Unfortunately this method is not available anymore, and I'm not sure what's the next step.

image

The error I'm seeing in my editor:
image

I've used both Supabase and Clerk's documentation on this integration, none are working at the moment.

Edit: The docs stated this setAuth is now deprecated. Would appreciate on updating this, as I still haven't found a way yet to set this up.

Edit 2:
The official docs in Clerk is the solution:
https://clerk.com/docs/integrations/databases/supabase#fetch-supabase-data-in-your-code

Only caveat is you need to import useRef and useState from react, otherwise the token would be undefined as it wasn't hydrated yet.

from supabase.

Xavier-IV avatar Xavier-IV commented on September 26, 2024

Hi, I would like to add on this.

In "Step 5: Set auth token with Supabase", it is mentioned to use supabase.auth.setAuth(token).

Following up to this, I just found a solution, but it feels hacky. Each time we need to call supabase, I made it to accept token as a param.

export const supabase = (clerkToken: string) =>
  createClient<Database>(
    process.env.NEXT_PUBLIC_SUPABASE_URL ?? "",
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? "",
    {
      global: {
        fetch: async (url, options = {}) => {
          const headers = new Headers(options?.headers);
          headers.set("Authorization", `Bearer ${clerkToken}`);

          return fetch(url, {
            ...options,
            headers,
          });
        },
      },
    },
  );
"use client";

export default function Component() {
  const { getToken } = useAuth();
  const [token, setToken] = useState('');

  // Where the magic happen, and leveraging useState
  const myQuery = supabase(token).from('my_table').select();

  useEffect(() => {
    const token = await getToken();
    if (!token) return;

    setToken(token);
    const data = await myQuery;

   // continue with your data
  }, []}
}

The problem with official docs using window.Clerk?.session.getToken() is that the token was null at first. Only once its fully loaded, we can leverage the token (thus the above solution).

from supabase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.