supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

[supabase-js v2] bring back the `auth.setAuth()` method

Open julianpoma opened this issue 4 years ago • 2 comments

Feature request

Bring back the ability to manually scope the supabase request to a particular JWT.

Is your feature request related to a problem? Please describe.

The following pattern is really useful when doing queries on next-js api routes or getServerSideProps function: lets you query data scoped to the current user (ie: RLS) without the need to rely on a supabaseAdminClient

export async function supabaseServerClient(
  context
) {
  const supabaseClient = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    {
      auth: {
        autoRefreshToken: false,
        detectSessionInUrl: false,
        persistSession: false
      }
    }
  )

  const access_token = context.req.cookies['supabase-access-token']

  if (!access_token) throw new AuthSessionMissingError()

  // Previously we where able to do this...

  const { error } = supabaseClient.auth.setAuth(access_token)

  if (error) throw error

  return supabaseClient
}

// On an API function or getServerSideProps
export default function handler({req, res}) {
  const supabase = await supabaseServerClient({ req })

  // queries will be scoped to the user
  const { data } = await supabase.from('table')...

}

Describe the solution you'd like

Implement a method to manually set the jwt token.

Describe alternatives you've considered

I can see that in the new supabase-v2 there is a setSession() method which takes the refresh_token and tries to generate a new token with that.

export async function supabaseServerClient(context) {
  const supabaseClient = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    {
      auth: {
        autoRefreshToken: false,
        detectSessionInUrl: false,
        persistSession: false,
      },
    }
  );

  // This is not working for me. Also will this invalidate the current session on the client?

  const refresh_token = context.req.cookies["my-app-refresh-token"];

  if (!refresh_token) throw new AuthSessionMissingError();

  const { error } = await supabaseClient.auth.setSession(refresh_token);

  // Outputs: AuthApiError: Invalid Refresh Token
  console.log("setSession error", error);

  if (error) throw error;

  return supabaseClient;
}

What I worry about this is approach is:

  • Calling this method on the backend invalidates the current session of the client (the JWT token no longer being valid)?
  • Manually calling setSession() is not working for me ATM, I always get an Invalid refresh token error, but that could be an issue on my end.

Additional context


julianpoma avatar Aug 17 '22 17:08 julianpoma

Hi there, have you seen this page? https://supabase.com/docs/reference/javascript/next/release-notes#deprecated-setauth

To set a custom access_token jwt instead, pass the custom header into the createClient() method provided: (PR)

Here is a link to an example implemenation: https://github.com/supabase/gotrue-js/pull/340#issuecomment-1218065610

So you could do something like this

export async function supabaseServerClient(
  context
) {
  const access_token = context.req.cookies['supabase-access-token']

  if (!access_token) throw new AuthSessionMissingError()

  const supabaseClient = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    {
      auth: {
        autoRefreshToken: false,
        detectSessionInUrl: false,
        persistSession: false
      },
      global: {
        headers: {
          Authorization: `Bearer ${access_token}`
        }
      }
    }
  )

  return supabaseClient
}

// On an API function or getServerSideProps
export default function handler({req, res}) {
  const supabase = await supabaseServerClient({ req })

  // queries will be scoped to the user
  const { data } = await supabase.from('table')...

}

Maybe this helps you :)

coinbird-development avatar Aug 17 '22 21:08 coinbird-development

Oh, thats great. TYSM! @kryptovergleichde

julianpoma avatar Aug 17 '22 21:08 julianpoma