Skip to content

Commit a2a94d1

Browse files
authored
Revert "Update auth hooks to use new endpoint and add perms checking" (supabase#29893)
Revert "Update auth hooks to use new endpoint and add perms checking (supabase#29870)" This reverts commit a3e1fb2.
1 parent a3e1fb2 commit a2a94d1

File tree

9 files changed

+142
-434
lines changed

9 files changed

+142
-434
lines changed

apps/studio/components/interfaces/Auth/Hooks/AddHookDropdown.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
DropdownMenuLabel,
1717
DropdownMenuSeparator,
1818
DropdownMenuTrigger,
19+
SheetSection,
1920
} from 'ui'
2021
import { HOOKS_DEFINITIONS, HOOK_DEFINITION_TITLE, Hook } from './hooks.constants'
2122
import { extractMethod, isValidHook } from './hooks.utils'
@@ -37,7 +38,7 @@ export const AddHookDropdown = ({
3738
{ enabled: IS_PLATFORM }
3839
)
3940
const { data: authConfig } = useAuthConfigQuery({ projectRef })
40-
const canUpdateAuthHook = useCheckPermissions(PermissionAction.AUTH_EXECUTE, '*')
41+
const canUpdateConfig = useCheckPermissions(PermissionAction.UPDATE, 'custom_config_gotrue')
4142

4243
const hooks: Hook[] = HOOKS_DEFINITIONS.map((definition) => {
4344
return {
@@ -56,7 +57,7 @@ export const AddHookDropdown = ({
5657
const isTeamsOrEnterprisePlan =
5758
subscription?.plan.id === 'team' || subscription?.plan.id === 'enterprise'
5859

59-
if (!canUpdateAuthHook) {
60+
if (!canUpdateConfig) {
6061
return (
6162
<ButtonTooltip
6263
disabled
@@ -77,7 +78,7 @@ export const AddHookDropdown = ({
7778
{buttonText}
7879
</Button>
7980
</DropdownMenuTrigger>
80-
<DropdownMenuContent className="w-76" align="end">
81+
<DropdownMenuContent className="w-76 p-0" align="end">
8182
<div>
8283
{nonEnterpriseHookOptions.map((h) => (
8384
<DropdownMenuItem key={h.title} onClick={() => onSelectHook(h.title)}>
@@ -86,7 +87,7 @@ export const AddHookDropdown = ({
8687
))}
8788
</div>
8889

89-
<DropdownMenuSeparator />
90+
<DropdownMenuSeparator className="my-0" />
9091

9192
{!isTeamsOrEnterprisePlan && (
9293
<DropdownMenuLabel className="grid gap-1 bg-surface-200">

apps/studio/components/interfaces/Auth/Hooks/CreateHookSheet.tsx

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import CodeEditor from 'components/ui/CodeEditor/CodeEditor'
1414
import FunctionSelector from 'components/ui/FunctionSelector'
1515
import SchemaSelector from 'components/ui/SchemaSelector'
1616
import { AuthConfigResponse } from 'data/auth/auth-config-query'
17-
import { useAuthHooksUpdateMutation } from 'data/auth/auth-hooks-update-mutation'
17+
import { useAuthConfigUpdateMutation } from 'data/auth/auth-config-update-mutation'
1818
import { executeSql } from 'data/sql/execute-sql-query'
1919
import { useFlag } from 'hooks/ui/useFlag'
2020
import {
@@ -42,10 +42,10 @@ import { extractMethod, getRevokePermissionStatements, isValidHook } from './hoo
4242

4343
interface CreateHookSheetProps {
4444
visible: boolean
45-
title: HOOK_DEFINITION_TITLE | null
46-
authConfig: AuthConfigResponse
4745
onClose: () => void
4846
onDelete: () => void
47+
title: HOOK_DEFINITION_TITLE | null
48+
authConfig: AuthConfigResponse
4949
}
5050

5151
export function generateAuthHookSecret() {
@@ -109,13 +109,14 @@ const FormSchema = z
109109

110110
export const CreateHookSheet = ({
111111
visible,
112-
title,
113-
authConfig,
114112
onClose,
115113
onDelete,
114+
title,
115+
authConfig,
116116
}: CreateHookSheetProps) => {
117117
const { ref: projectRef } = useParams()
118118
const { project } = useProjectContext()
119+
const { mutate: updateAuthConfig, isLoading: isUpdatingConfig } = useAuthConfigUpdateMutation()
119120
const httpsAuthHooksEnabled = useFlag('httpsAuthHooksEnabled')
120121

121122
const definition = useMemo(
@@ -154,6 +155,45 @@ export const CreateHookSheet = ({
154155
},
155156
})
156157

158+
useEffect(() => {
159+
if (visible) {
160+
if (definition) {
161+
const values = extractMethod(
162+
authConfig?.[definition.uriKey] || '',
163+
authConfig?.[definition.secretsKey] || ''
164+
)
165+
166+
form.reset({
167+
hookType: definition.title,
168+
enabled: authConfig?.[definition.enabledKey] || false,
169+
selectedType: values.type,
170+
httpsValues: {
171+
url: (values.type === 'https' && values.url) || '',
172+
secret: (values.type === 'https' && values.secret) || '',
173+
},
174+
postgresValues: {
175+
schema: (values.type === 'postgres' && values.schema) || 'public',
176+
functionName: (values.type === 'postgres' && values.functionName) || '',
177+
},
178+
})
179+
} else {
180+
form.reset({
181+
hookType: title || '',
182+
enabled: true,
183+
selectedType: 'postgres',
184+
httpsValues: {
185+
url: '',
186+
secret: '',
187+
},
188+
postgresValues: {
189+
schema: 'public',
190+
functionName: '',
191+
},
192+
})
193+
}
194+
}
195+
}, [authConfig, title, visible, definition])
196+
157197
const values = form.watch()
158198

159199
const statements = useMemo(() => {
@@ -182,23 +222,6 @@ export const CreateHookSheet = ({
182222
return permissionChanges
183223
}, [hook, values.postgresValues.schema, values.postgresValues.functionName])
184224

185-
const { mutate: updateAuthHooks, isLoading: isUpdatingAuthHooks } = useAuthHooksUpdateMutation({
186-
onSuccess: () => {
187-
toast.success(`Successfully created ${values.hookType}.`)
188-
if (statements.length > 0) {
189-
executeSql({
190-
projectRef,
191-
connectionString: project!.connectionString,
192-
sql: statements.join('\n'),
193-
})
194-
}
195-
onClose()
196-
},
197-
onError: (error) => {
198-
toast.error(`Failed to create hook: ${error.message}`)
199-
},
200-
})
201-
202225
const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (values) => {
203226
if (!project) return console.error('Project is required')
204227
const definition = HOOKS_DEFINITIONS.find((d) => values.hookType === d.title)
@@ -224,47 +247,27 @@ export const CreateHookSheet = ({
224247
[secretsLabel]: values.selectedType === 'https' ? values.httpsValues.secret : null,
225248
}
226249

227-
updateAuthHooks({ projectRef: projectRef!, config: payload })
228-
}
229-
230-
useEffect(() => {
231-
if (visible) {
232-
if (definition) {
233-
const values = extractMethod(
234-
authConfig?.[definition.uriKey] || '',
235-
authConfig?.[definition.secretsKey] || ''
236-
)
237-
238-
form.reset({
239-
hookType: definition.title,
240-
enabled: authConfig?.[definition.enabledKey] || false,
241-
selectedType: values.type,
242-
httpsValues: {
243-
url: (values.type === 'https' && values.url) || '',
244-
secret: (values.type === 'https' && values.secret) || '',
245-
},
246-
postgresValues: {
247-
schema: (values.type === 'postgres' && values.schema) || 'public',
248-
functionName: (values.type === 'postgres' && values.functionName) || '',
249-
},
250-
})
251-
} else {
252-
form.reset({
253-
hookType: title || '',
254-
enabled: true,
255-
selectedType: 'postgres',
256-
httpsValues: {
257-
url: '',
258-
secret: '',
259-
},
260-
postgresValues: {
261-
schema: 'public',
262-
functionName: '',
263-
},
264-
})
250+
updateAuthConfig(
251+
{ projectRef: projectRef!, config: payload },
252+
{
253+
onSuccess: () => {
254+
toast.success(`Successfully created ${values.hookType}.`)
255+
if (statements.length > 0) {
256+
executeSql({
257+
projectRef,
258+
connectionString: project.connectionString,
259+
sql: statements.join('\n'),
260+
})
261+
}
262+
onClose()
263+
},
264+
onError: (error) => {
265+
toast.error(`Failed to create hook: ${error.message}`)
266+
onClose()
267+
},
265268
}
266-
}
267-
}, [authConfig, title, visible, definition])
269+
)
270+
}
268271

269272
return (
270273
<Sheet open={visible} onOpenChange={() => onClose()}>
@@ -489,16 +492,16 @@ export const CreateHookSheet = ({
489492
</div>
490493
)}
491494

492-
<Button disabled={isUpdatingAuthHooks} type="default" onClick={() => onClose()}>
495+
<Button disabled={isUpdatingConfig} type="default" onClick={() => onClose()}>
493496
Cancel
494497
</Button>
495498
<Button
496499
form={FORM_ID}
497500
htmlType="submit"
498-
disabled={isUpdatingAuthHooks}
499-
loading={isUpdatingAuthHooks}
501+
disabled={isUpdatingConfig}
502+
loading={isUpdatingConfig}
500503
>
501-
{isCreating ? 'Create hook' : 'Update hook'}
504+
{isCreating ? 'Create' : 'Update'}
502505
</Button>
503506
</SheetFooter>
504507
</SheetContent>

apps/studio/components/interfaces/Auth/Hooks/HookCard.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { PermissionAction } from '@supabase/shared-types/out/constants'
21
import { Check, Webhook } from 'lucide-react'
32
import { Badge, Input } from 'ui'
43

54
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
6-
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
75
import { copyToClipboard } from 'lib/helpers'
86
import { Hook } from './hooks.constants'
97

108
interface HookCardProps {
119
hook: Hook
10+
canUpdateConfig: boolean
11+
onToggle: (enabled: boolean) => void
1212
onSelect: () => void
1313
}
1414

15-
export const HookCard = ({ hook, onSelect }: HookCardProps) => {
16-
const canUpdateAuthHook = useCheckPermissions(PermissionAction.AUTH_EXECUTE, '*')
17-
15+
export const HookCard = ({ hook, canUpdateConfig, onToggle, onSelect }: HookCardProps) => {
1816
return (
1917
<div className="bg-surface-100 border-default overflow-hidden border shadow px-5 py-4 flex flex-row first:rounded-t-md last:rounded-b-md space-x-4">
2018
<div className="">
@@ -95,7 +93,7 @@ export const HookCard = ({ hook, onSelect }: HookCardProps) => {
9593
<div className="flex flex-row gap-2">
9694
<ButtonTooltip
9795
type="default"
98-
disabled={!canUpdateAuthHook}
96+
disabled={!canUpdateConfig}
9997
onClick={() => onSelect()}
10098
tooltip={{
10199
content: {

0 commit comments

Comments
 (0)