Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic header does not change after a client has been instantiated #1280

Closed
uncvrd opened this issue Oct 12, 2024 · 2 comments
Closed

Dynamic header does not change after a client has been instantiated #1280

uncvrd opened this issue Oct 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@uncvrd
Copy link

uncvrd commented Oct 12, 2024

Bug report

  • [ x] I confirm this is a bug with Supabase, not with my own application.
  • [ x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I need to send headers with my client requests. To do that I have configured the following:

export function createClient(orgContactId: string | null) {
    const supabase = createBrowserClient<Database>(env.NEXT_PUBLIC_SUPABASE_PROJECT_URL, env.NEXT_PUBLIC_SUPABASE_ANON_KEY, {
        cookies: {},
        cookieOptions: {
            domain: env.NEXT_PUBLIC_APP_DOMAIN,
        },
        global: {
            headers: {
                "org-contact-id": `${orgContactId ?? ""}`,
            },
        },
        isSingleton: false,
    })

    return supabase
}

However the issue is that I can't update this header after the client is created so if the orgContactId changes, I need to create a completely new client

I do that by taking the "singleton" in to my own hands and when I detect an ID change, I create a new client that has the new header

export let supabaseBrowserClient: SupabaseClient<Database> | null = null
let orgContactId: string | null = null

export function getSupabaseBrowserClient(activeOrgContactId: string | null) {
    if (supabaseBrowserClient && activeOrgContactId === orgContactId) {
        return supabaseBrowserClient
    }

    orgContactId = activeOrgContactId
    supabaseBrowserClient = createClient(activeOrgContactId)

    return supabaseBrowserClient
}

function useSupabaseBrowser() {
    const {
        userPreference: { activeOrgContactId },
    } = useAppStore((store) => store)

    return useMemo(() => getSupabaseBrowserClient(activeOrgContactId), [activeOrgContactId])
}

The downside to this is that I get warnings in my console stating there are now multiple clients created and that I may deal with unintended behavior.

I think I'm running in to that unintended behavior now. I use NextJS and just tried running my dev server with next dev --turbo. For some reason, my supabase client will only ever use my first instantiated version of the client (which doesnt contain a org-contact-id) even if a few seconds later a new auth client is instantiated with an org-contact-id. It simply just isn't used for any of my requests (the header is empty since it was the first client instantiated it seems...)

I would love to be able to just simply update the headers in the client after it's created...if possible.

Hope you see the use case for it, let me know if you have any other questions :)

Meant to send this months ago but the recommendation came from this auth/ssr discussion: https://github.com/orgs/supabase/discussions/27037

System information

  • OS: macOS
  • Version of supabase-js: 2.43.5
  • Version of Node.js: 20.12.0
@uncvrd uncvrd added the bug Something isn't working label Oct 12, 2024
@soedirgo
Copy link
Member

@soedirgo
Copy link
Member

Closing this for now - feel free to reopen or create a new issue if the proposed solution doesn't work.

@soedirgo soedirgo closed this as not planned Won't fix, can't repro, duplicate, stale Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants