The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix: Fix the role impersonation in the realtime inspector (#22725)
Ivan Vasilov446661975d91
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/RealtimeTokensPopover/index.tsx b/apps/studio/components/interfaces/Realtime/Inspector/RealtimeTokensPopover/index.tsxindex 765f89444a1..d874a9c8e92 100644--- a/apps/studio/components/interfaces/Realtime/Inspector/RealtimeTokensPopover/index.tsx+++ b/apps/studio/components/interfaces/Realtime/Inspector/RealtimeTokensPopover/index.tsx@@ -28,9 +28,8 @@ export const RealtimeTokensPopover = ({ config, onChangeConfig }: RealtimeTokens: undefinedconst anonRoleKey = apiService?.service_api_keys.find((x) => x.name === 'anon key')- ? apiService.serviceApiKey+ ? apiService.defaultApiKey: undefined-const { data: postgrestConfig } = useProjectPostgrestConfigQuery({projectRef: config.projectRef,@@ -59,27 +58,30 @@ export const RealtimeTokensPopover = ({ config, onChangeConfig }: RealtimeTokens}, [snap.role])useEffect(() => {- let token: string | undefined- let bearer: string | null = null+ const triggerUpdateTokenBearer = async () => {+ let token: string | undefined+ let bearer: string | null = null- if (- config.projectRef !== undefined &&- jwtSecret !== undefined &&- snap.role !== undefined &&- snap.role.type === 'postgrest'- ) {- token = anonRoleKey- getRoleImpersonationJWT(config.projectRef, jwtSecret, snap.role)- .then((token) => (bearer = token))- .catch((err) => toast.error(`Failed to get JWT for role: ${err.message}`))- } else {- token = serviceRoleKey+ if (+ config.projectRef !== undefined &&+ jwtSecret !== undefined &&+ snap.role !== undefined &&+ snap.role.type === 'postgrest'+ ) {+ token = anonRoleKey+ await getRoleImpersonationJWT(config.projectRef, jwtSecret, snap.role)+ .then((b) => (bearer = b))+ .catch((err) => toast.error(`Failed to get JWT for role: ${err.message}`))+ } else {+ token = serviceRoleKey+ }+ if (token) {+ onChangeConfig({ ...config, token, bearer })+ }}- if (token) {- onChangeConfig({ ...config, token, bearer })- }- }, [config.projectRef, jwtSecret, serviceRoleKey, snap.role])+ triggerUpdateTokenBearer()+ }, [snap.role, anonRoleKey, serviceRoleKey])return <RoleImpersonationPopover align="start" variant="connected-on-both" />}diff --git a/apps/studio/components/interfaces/Realtime/Inspector/index.tsx b/apps/studio/components/interfaces/Realtime/Inspector/index.tsxindex 010e985a032..718ae8ec315 100644--- a/apps/studio/components/interfaces/Realtime/Inspector/index.tsx+++ b/apps/studio/components/interfaces/Realtime/Inspector/index.tsx@@ -32,7 +32,7 @@ export const RealtimeInspector = () => {enableBroadcast: true,})- const { logData, sendMessage } = useRealtimeMessages(realtimeConfig)+ const { logData, sendMessage } = useRealtimeMessages(realtimeConfig, setRealtimeConfig)return (<div className="flex flex-col grow h-full">diff --git a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.tsindex 6e3dd7a23ab..cb4c57e7e18 100644--- a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts+++ b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts@@ -1,6 +1,6 @@import { RealtimeChannel, SupabaseClient } from '@supabase/supabase-js'import { sortBy, take } from 'lodash'-import { useCallback, useEffect, useReducer, useState } from 'react'+import { Dispatch, SetStateAction, useCallback, useEffect, useReducer, useState } from 'react'import toast from 'react-hot-toast'import { useProjectApiQuery } from 'data/config/project-api-query'@@ -50,20 +50,25 @@ export interface RealtimeConfig {enableDbChanges: boolean}-export const useRealtimeMessages = ({- enabled,- channelName,- projectRef,- logLevel,- token,- schema,- table,- filter,- bearer,- enablePresence,- enableDbChanges,- enableBroadcast,-}: RealtimeConfig) => {+export const useRealtimeMessages = (+ config: RealtimeConfig,+ setRealtimeConfig: Dispatch<SetStateAction<RealtimeConfig>>+) => {+ const {+ enabled,+ channelName,+ projectRef,+ logLevel,+ token,+ schema,+ table,+ filter,+ bearer,+ enablePresence,+ enableDbChanges,+ enableBroadcast,+ } = config+const { data } = useProjectApiQuery({ projectRef: projectRef })// the default host is prod until the correct one comes through an API call.@@ -97,7 +102,8 @@ export const useRealtimeMessages = ({},}const newClient = new SupabaseClient(host, token, opts)- if (bearer != '') {++ if (bearer) {newClient.realtime.setAuth(bearer)}@@ -170,6 +176,14 @@ export const useRealtimeMessages = ({}} else if (status === 'CLOSED') {// console.log(`Realtime Channel status: ${status}`)+ } else if (status === 'CHANNEL_ERROR') {+ toast.error(+ 'Failed to connect to the channel: This may be due to restrictive RLS policies. Check your role and try again.'+ )++ newChannel.unsubscribe()+ setChannel(undefined)+ setRealtimeConfig({ ...config, enabled: false })} else {// console.error(`Realtime Channel error status: ${status}`)// console.error(`Realtime Channel error: ${error}`)