Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

param routing: oAuth apps and hooks (#40522)

Francesco Sansalvadore622609d73427
diff --git a/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx b/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx
index 5a89a957b24..9e07fcae460 100644
--- a/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx
+++ b/apps/studio/components/interfaces/Auth/Hooks/HooksListing.tsx
@@ -8,6 +8,7 @@ import CodeEditor from 'components/ui/CodeEditor/CodeEditor'
import { useAuthConfigQuery } from 'data/auth/auth-config-query'
import { useAuthHooksUpdateMutation } from 'data/auth/auth-hooks-update-mutation'
import { executeSql } from 'data/sql/execute-sql-query'
+import { useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { cn } from 'ui'
import { GenericSkeletonLoader } from 'ui-patterns'
@@ -15,7 +16,7 @@ import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import { AddHookDropdown } from './AddHookDropdown'
import { CreateHookSheet } from './CreateHookSheet'
import { HookCard } from './HookCard'
-import { HOOKS_DEFINITIONS, HOOK_DEFINITION_TITLE, Hook } from './hooks.constants'
+import { HOOKS_DEFINITIONS, Hook } from './hooks.constants'
import { extractMethod, getRevokePermissionStatements, isValidHook } from './hooks.utils'
export const HooksListing = () => {
@@ -28,7 +29,6 @@ export const HooksListing = () => {
isLoading,
} = useAuthConfigQuery({ projectRef })
- const [selectedHook, setSelectedHook] = useState<HOOK_DEFINITION_TITLE | null>(null)
const [selectedHookForDeletion, setSelectedHookForDeletion] = useState<Hook | null>(null)
const { mutate: updateAuthHooks, isLoading: isDeletingAuthHook } = useAuthHooksUpdateMutation({
@@ -64,6 +64,17 @@ export const HooksListing = () => {
}
})
+ const { setValue: setSelectedHook, value: selectedHook } = useQueryStateWithSelect({
+ urlKey: 'hook',
+ select: (id: string) => {
+ if (!id) return null
+ const hook = hooks.find((h) => h.id === id)
+ return hook ? hook.title : undefined
+ },
+ enabled: !!hooks && hooks.length > 0,
+ onError: () => toast.error(`Hook not found`),
+ })
+
if (isError) {
return (
<ScaffoldSection isFullWidth>
@@ -87,7 +98,12 @@ export const HooksListing = () => {
<ScaffoldSection isFullWidth>
<div className="flex justify-between items-center mb-4">
<ScaffoldSectionTitle>All hooks</ScaffoldSectionTitle>
- <AddHookDropdown onSelectHook={setSelectedHook} />
+ <AddHookDropdown
+ onSelectHook={(title) => {
+ const hook = hooks.find((h) => h.title === title)
+ if (hook) setSelectedHook(hook.id)
+ }}
+ />
</div>
{hooks.filter((h) => isValidHook(h)).length === 0 && (
@@ -101,7 +117,10 @@ export const HooksListing = () => {
<AddHookDropdown
align="center"
buttonText="Add a new hook"
- onSelectHook={setSelectedHook}
+ onSelectHook={(title) => {
+ const hook = hooks.find((h) => h.title === title)
+ if (hook) setSelectedHook(hook.id)
+ }}
/>
</div>
)}
@@ -114,14 +133,14 @@ export const HooksListing = () => {
<HookCard
key={hook.enabledKey}
hook={hook}
- onSelect={() => setSelectedHook(hook.title)}
+ onSelect={() => setSelectedHook(hook.id)}
/>
)
})}
</div>
<CreateHookSheet
- title={selectedHook}
+ title={selectedHook ?? null}
visible={!!selectedHook}
onDelete={() => {
const hook = hooks.find((h) => h.title === selectedHook)
diff --git a/apps/studio/components/interfaces/Auth/Hooks/hooks.constants.ts b/apps/studio/components/interfaces/Auth/Hooks/hooks.constants.ts
index 7002b60ab4e..eb5234874aa 100644
--- a/apps/studio/components/interfaces/Auth/Hooks/hooks.constants.ts
+++ b/apps/studio/components/interfaces/Auth/Hooks/hooks.constants.ts
@@ -1,5 +1,6 @@
export const HOOKS_DEFINITIONS = [
{
+ id: 'send-sms',
title: 'Send SMS hook',
subtitle: 'Will be called by Supabase Auth each time an SMS message needs to be sent.',
enabledKey: 'HOOK_SEND_SMS_ENABLED',
@@ -9,6 +10,7 @@ export const HOOKS_DEFINITIONS = [
docSlug: 'send-sms-hook',
},
{
+ id: 'send-email',
title: 'Send Email hook',
subtitle: 'Will be called by Supabase Auth each time an email message needs to be sent.',
enabledKey: 'HOOK_SEND_EMAIL_ENABLED',
@@ -18,6 +20,7 @@ export const HOOKS_DEFINITIONS = [
docSlug: 'send-email-hook',
},
{
+ id: 'custom-access-token-claims',
title: 'Customize Access Token (JWT) Claims hook',
subtitle:
'Will be called by Supabase Auth each time a new JWT is created. It should return the claims you wish to be present in the JWT.',
@@ -28,6 +31,7 @@ export const HOOKS_DEFINITIONS = [
docSlug: 'custom-access-token-hook',
},
{
+ id: 'mfa-verification-attempt',
title: 'MFA Verification Attempt hook',
subtitle:
'Will be called by Supabase Auth each time a user tries to verify an MFA factor. Return a decision on whether to reject the attempt and future ones, or to allow the user to keep trying.',
@@ -38,6 +42,7 @@ export const HOOKS_DEFINITIONS = [
docSlug: 'mfa-verification-hook',
},
{
+ id: 'password-verification-attempt',
title: 'Password Verification Attempt hook',
subtitle:
'Will be called by Supabase Auth each time a user tries to sign in with a password. Return a decision whether to allow the user to reject the attempt, or to allow the user to keep trying.',
@@ -48,6 +53,7 @@ export const HOOKS_DEFINITIONS = [
docSlug: 'password-verification-hook',
},
{
+ id: 'before-user-created',
title: 'Before User Created hook',
subtitle:
'Will be called by Supabase Auth before creating a new user. Returning an error will prevent the user from being created.',
@@ -62,6 +68,7 @@ export const HOOKS_DEFINITIONS = [
export type HOOK_DEFINITION_TITLE = (typeof HOOKS_DEFINITIONS)[number]['title']
export interface Hook {
+ id: string
title: HOOK_DEFINITION_TITLE
subtitle: string
enabled: boolean
diff --git a/apps/studio/components/interfaces/Auth/OAuthApps/DeleteOAuthAppModal.tsx b/apps/studio/components/interfaces/Auth/OAuthApps/DeleteOAuthAppModal.tsx
index 990dc47cba1..acd888f8aee 100644
--- a/apps/studio/components/interfaces/Auth/OAuthApps/DeleteOAuthAppModal.tsx
+++ b/apps/studio/components/interfaces/Auth/OAuthApps/DeleteOAuthAppModal.tsx
@@ -1,57 +1,44 @@
import type { OAuthClient } from '@supabase/supabase-js'
import { useParams } from 'common'
-import { useOAuthServerAppDeleteMutation } from 'data/oauth-server-apps/oauth-server-app-delete-mutation'
+import type { OAuthServerAppDeleteVariables } from 'data/oauth-server-apps/oauth-server-app-delete-mutation'
import { useSupabaseClientQuery } from 'hooks/use-supabase-client-query'
-import { useState } from 'react'
-import { toast } from 'sonner'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
interface DeleteOAuthAppModalProps {
visible: boolean
selectedApp?: OAuthClient
- onClose: () => void
+ setVisible: (value: string | null) => void
+ onDelete: (params: OAuthServerAppDeleteVariables) => void
+ isLoading: boolean
}
export const DeleteOAuthAppModal = ({
visible,
selectedApp,
- onClose,
+ setVisible,
+ onDelete,
+ isLoading,
}: DeleteOAuthAppModalProps) => {
const { ref: projectRef } = useParams()
- const [isDeleting, setIsDeleting] = useState(false)
const { data: supabaseClientData } = useSupabaseClientQuery({ projectRef })
- const { mutateAsync: deleteOAuthApp } = useOAuthServerAppDeleteMutation()
+ const onConfirmDeleteApp = () => {
+ if (!selectedApp) return
- const onConfirmDeleteApp = async () => {
- if (!selectedApp) return console.error('No OAuth app selected')
-
- setIsDeleting(true)
-
- try {
- await deleteOAuthApp({
- projectRef,
- supabaseClient: supabaseClientData?.supabaseClient,
- clientId: selectedApp.client_id,
- })
-
- toast.success(`Successfully deleted OAuth app "${selectedApp.client_name}"`)
- onClose()
- } catch (error) {
- toast.error('Failed to delete OAuth app')
- console.error('Error deleting OAuth app:', error)
- } finally {
- setIsDeleting(false)
- }
+ onDelete({
+ projectRef,
+ supabaseClient: supabaseClientData?.supabaseClient,
+ clientId: selectedApp.client_id,
+ })
}
return (
<ConfirmationModal
variant={'destructive'}
size="medium"
- loading={isDeleting}
+ loading={isLoading}
visible={visible}
title={
<>
@@ -60,7 +47,7 @@ export const DeleteOAuthAppModal = ({
}
confirmLabel="Confirm delete"
confirmLabelLoading="Deleting..."
- onCancel={onClose}
+ onCancel={() => setVisible(null)}
onConfirm={() => onConfirmDeleteApp()}
alert={{
title: 'This action cannot be undone',
diff --git a/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx b/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
index 337b601c72b..49c65bb2086 100644
--- a/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
+++ b/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
@@ -1,7 +1,9 @@
import type { OAuthClient } from '@supabase/supabase-js'
import { MoreVertical, Plus, RotateCw, Search, Trash } from 'lucide-react'
import Link from 'next/link'
-import { useState } from 'react'
+import { useRef, useState } from 'react'
+import { parseAsBoolean, useQueryState } from 'nuqs'
+import { toast } from 'sonner'
import { useParams } from 'common'
import AlertError from 'components/ui/AlertError'
@@ -9,9 +11,11 @@ import { ButtonTooltip } from 'components/ui/ButtonTooltip'
import { FilterPopover } from 'components/ui/FilterPopover'
import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
import { useAuthConfigQuery } from 'data/auth/auth-config-query'
+import { useOAuthServerAppDeleteMutation } from 'data/oauth-server-apps/oauth-server-app-delete-mutation'
import { useOAuthServerAppRegenerateSecretMutation } from 'data/oauth-server-apps/oauth-server-app-regenerate-secret-mutation'
import { useOAuthServerAppsQuery } from 'data/oauth-server-apps/oauth-server-apps-query'
import { useSupabaseClientQuery } from 'hooks/use-supabase-client-query'
+import { useQueryStateWithSelect, handleErrorOnDelete } from 'hooks/misc/useQueryStateWithSelect'
import {
Badge,
Button,
@@ -51,14 +55,11 @@ export const OAuthAppsList = () => {
const { data: authConfig, isLoading: isAuthConfigLoading } = useAuthConfigQuery({ projectRef })
const isOAuthServerEnabled = !!authConfig?.OAUTH_SERVER_ENABLED
const [newOAuthApp, setNewOAuthApp] = useState<OAuthClient | undefined>(undefined)
-
- // State for OAuth apps
- const [showCreateSheet, setShowCreateSheet] = useState(false)
- const [showDeleteModal, setShowDeleteModal] = useState(false)
const [showRegenerateDialog, setShowRegenerateDialog] = useState(false)
const [selectedApp, setSelectedApp] = useState<OAuthClient>()
const [filteredAppTypes, setFilteredAppTypes] = useState<string[]>([])
const [filteredAppScopes, setFilteredAppScopes] = useState<string[]>([])
+ const deletingOAuthAppIdRef = useRef<string | null>(null)
const { data: supabaseClientData } = useSupabaseClientQuery({ projectRef })
@@ -81,9 +82,32 @@ export const OAuthAppsList = () => {
const oAuthApps = data?.clients || []
+ const [showCreateSheet, setShowCreateSheet] = useQueryState(
+ 'new',
+ parseAsBoolean.withDefault(false).withOptions({ history: 'push', clearOnDefault: true })
+ )
+
+ const { setValue: setSelectedAppToDelete, value: appToDelete } = useQueryStateWithSelect({
+ urlKey: 'delete',
+ select: (client_id: string) =>
+ client_id ? oAuthApps?.find((app) => app.client_id === client_id) : undefined,
+ enabled: !!oAuthApps?.length,
+ onError: (_error, selectedId) =>
+ handleErrorOnDelete(deletingOAuthAppIdRef, selectedId, `OAuth App not found`),
+ })
+
+ const { mutate: deleteOAuthApp, isLoading: isDeletingApp } = useOAuthServerAppDeleteMutation({
+ onSuccess: () => {
+ toast.success(`Successfully deleted OAuth app`)
+ setSelectedAppToDelete(null)
+ },
+ onError: () => {
+ deletingOAuthAppIdRef.current = null
+ },
+ })
+
const handleDeleteClick = (app: OAuthClient) => {
- setSelectedApp(app)
- setShowDeleteModal(true)
+ setSelectedAppToDelete(app.client_id)
}
const [filterString, setFilterString] = useState<string>('')
@@ -205,7 +229,7 @@ export const OAuthAppsList = () => {
{app.client_name}
</TableCell>
<TableCell className="max-w-40" title={app.client_id}>
- <Badge>{app.client_id}</Badge>
+ <Badge className="font-mono">{app.client_id}</Badge>
</TableCell>
<TableCell className="max-w-40">
{app.client_type === 'public' ? 'Public' : 'Private'}
@@ -270,9 +294,14 @@ export const OAuthAppsList = () => {
/>
<DeleteOAuthAppModal
- visible={showDeleteModal}
- onClose={() => setShowDeleteModal(false)}
- selectedApp={selectedApp}
+ visible={!!appToDelete}
+ selectedApp={appToDelete}
+ setVisible={setSelectedAppToDelete}
+ onDelete={(params: Parameters<typeof deleteOAuthApp>[0]) => {
+ deletingOAuthAppIdRef.current = params.clientId ?? null
+ deleteOAuthApp(params)
+ }}
+ isLoading={isDeletingApp}
/>
<ConfirmationModal
@@ -283,11 +312,13 @@ export const OAuthAppsList = () => {
confirmLabel="Confirm"
onCancel={() => setShowRegenerateDialog(false)}
onConfirm={() => {
- regenerateSecret({
- projectRef,
- supabaseClient: supabaseClientData?.supabaseClient,
- clientId: selectedApp?.client_id!,
- })
+ if (selectedApp?.client_id) {
+ regenerateSecret({
+ projectRef,
+ supabaseClient: supabaseClientData?.supabaseClient,
+ clientId: selectedApp.client_id,
+ })
+ }
setShowRegenerateDialog(false)
}}
>