Files
supabase/apps/studio/hooks/use-supabase-client-query.ts
Francesco SansalvadoreandIvan Vasilov bc22be3855 feat: OAuth 2.1 - OAuth apps (#39165)
* oAuth clients index layout

* oAuth apps crud

* is public

* add user count and client secret generation and management

* scaffold oauth server settings

* improve oauth server enablement / disablement

* show cover when oAuth server is disabled

* fix update panel update button

* add site url and authorization path settings values

* move oauth server to it's own nav item

* remove unneeded oauth server settings

* let the user disactivate oauth server even after creating oauth apps

* better delete button

* cleanup

* fix typecheck

* test endpoints

* add EnableOAuth21 feature flag

* update OAUTH_SERVER_ auth config api

* load OAUTH_SERVER_ENABLED in oauth list

* Update the api.d.ts. Remove the custom versions of supa libs.

* Add query for getTemporaryAPIKey.

* Add a hook for initializing a supabase client.

* Add hooks for oAuth Server apps.

* Regenerate pnpm-lock.yaml.

* Revert updates to the platform.d.ts. Not needed for this PR.

* Migrate all code to use the new hooks.

* Try to integrate the mutations and fix some of the sheet and dialogs.

* improve default and saving states

* fix oauth app form validation

* unify components into CreateOrUpdateOAuthAppModal

* create or update oauth app

* Update the OAuth Server page.

* Remove extra files.

* Minor various fixes.

* More fixes to the creation of oauth apps.

* Bump the libs to fix a DELETE oauth app error.

* Clean up the scope feature.

* Move the feature flag in the auth layout.

* Bunch of smaller fixes.

* Regenerate pnpm-lock.

* Revert SidePanel and CardDescription changes.

* Add confirm dialog for regenerating secret.

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-10-30 17:07:07 +00:00

55 lines
1.6 KiB
TypeScript

import { createClient } from '@supabase/supabase-js'
import { useQuery } from '@tanstack/react-query'
import { useTemporaryAPIKeyQuery } from 'data/api-keys/temp-api-keys-query'
import { useProjectSettingsV2Query } from 'data/config/project-settings-v2-query'
const getSupabaseClient = ({
projectRef,
endpoint,
temporaryApiKey,
}: {
projectRef?: string
endpoint?: string
temporaryApiKey?: string
}) => {
if (!projectRef) {
return undefined
}
if (!endpoint) {
return undefined
}
if (temporaryApiKey === undefined) {
return undefined
}
const supabaseClient = createClient(endpoint, temporaryApiKey)
return { supabaseClient }
}
/**
* The client uses a temporary API key to authenticate requests. The API key expires after one hour which may cause
* 401 errors for all requests made with the client. It's easily fixable by refreshing the page.
*/
export const useSupabaseClientQuery = (
{ projectRef }: { projectRef?: string },
{ enabled = true, ...options } = {}
) => {
const { data: settings } = useProjectSettingsV2Query({ projectRef })
const { data: temporaryApiKeyData } = useTemporaryAPIKeyQuery({ projectRef, expiry: 3600 })
const endpoint = settings
? `${settings?.app_config?.protocol ?? 'https'}://${settings?.app_config?.endpoint}`
: undefined
const temporaryApiKey = temporaryApiKeyData?.api_key
return useQuery({
queryKey: [projectRef, 'supabase-client', endpoint, temporaryApiKey],
queryFn: () => getSupabaseClient({ projectRef, endpoint, temporaryApiKey }),
enabled: enabled && typeof projectRef !== 'undefined' && !!endpoint && !!temporaryApiKey,
...options,
})
}