workos / authkit-nextjs

The WorkOS library for Next.js provides convenient helpers for authentication and session management using WorkOS & AuthKit with Next.js.
MIT License
58 stars 14 forks source link

Is there a way to access the user token? #42

Closed kenhuang1964 closed 4 months ago

kenhuang1964 commented 4 months ago

I was using clerk for authentication with supabase but now I'm trying to switch to work os. Is there a way to access the user's token to work with supabase?

import { createClient } from "@supabase/supabase-js";
import { auth } from "@clerk/nextjs";

export default async function supabaseClient() {
  const { userId, getToken } = auth();
  if (!userId) throw new Error("User not authenticated");

  const accessToken = await getToken({ template: "supabase" });

  const supabase = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL ?? "",
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? "",
    {
      global: { headers: { Authorization: `Bearer ${accessToken}` } },
    }
  );
  return supabase;
}