Skip to content

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

Update check against CNAME record + sdhift to RQ

Joshen Lim86f7e2584a1c
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
index 2c79b3f2164..37b96f08c6a 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainActivate.tsx
@@ -14,11 +14,11 @@ import {
import ConfirmationModal from 'components/ui/ConfirmationModal'
import Panel from 'components/ui/Panel'
import { useProjectApiQuery } from 'data/config/project-api-query'
+import { useCheckCNAMERecordMutation } from 'data/custom-domains/check-cname-mutation'
import { useCustomDomainActivateMutation } from 'data/custom-domains/custom-domains-activate-mutation'
import { useCustomDomainDeleteMutation } from 'data/custom-domains/custom-domains-delete-mutation'
import { CustomDomainResponse } from 'data/custom-domains/custom-domains-query'
import { useStore } from 'hooks'
-import { verifyCNAME } from './CustomDomainConfig.utils'
export type CustomDomainActivateProps = {
projectRef?: string
@@ -27,43 +27,34 @@ export type CustomDomainActivateProps = {
const CustomDomainActivate = ({ projectRef, customDomain }: CustomDomainActivateProps) => {
const { ui } = useStore()
- const [isActivating, setIsActivating] = useState(false)
const [isActivateConfirmModalVisible, setIsActivateConfirmModalVisible] = useState(false)
const { data: settings } = useProjectApiQuery({ projectRef })
- const { mutate: activateCustomDomain } = useCustomDomainActivateMutation({
- onSuccess: () => {
- ui.setNotification({ category: 'success', message: `Successfully activated custom domain` })
- setIsActivateConfirmModalVisible(false)
+ const { mutate: checkCNAMERecord, isLoading: isCheckingRecord } = useCheckCNAMERecordMutation({
+ onError: () => {
+ return ui.setNotification({
+ category: 'error',
+ message: `Your CNAME record for ${customDomain.hostname} cannot be found - if you've just added the CNAME record, do check back in a bit.`,
+ })
},
})
+ const { mutate: activateCustomDomain, isLoading: isActivating } = useCustomDomainActivateMutation(
+ {
+ onSuccess: () => {
+ ui.setNotification({ category: 'success', message: `Successfully activated custom domain` })
+ setIsActivateConfirmModalVisible(false)
+ },
+ }
+ )
const { mutate: deleteCustomDomain, isLoading: isDeleting } = useCustomDomainDeleteMutation()
const endpoint = settings?.autoApiService.endpoint
const onActivateCustomDomain = async () => {
if (!projectRef) return console.error('Project ref is required')
-
- setIsActivating(true)
-
- const cnameVerified = await verifyCNAME(customDomain.hostname)
- if (!cnameVerified) {
- setIsActivating(false)
- return ui.setNotification({
- category: 'error',
- message: `Your CNAME record for ${customDomain.hostname} cannot be found - if you've just added the CNAME record, do check back in a bit.`,
- })
- }
- activateCustomDomain(
- { projectRef },
- {
- onSuccess: () => {
- setIsActivating(false)
- },
- onError: () => {
- setIsActivating(false)
- },
- }
+ checkCNAMERecord(
+ { domain: customDomain.hostname },
+ { onSuccess: () => activateCustomDomain({ projectRef }) }
)
}
@@ -161,7 +152,7 @@ const CustomDomainActivate = ({ projectRef, customDomain }: CustomDomainActivate
<ConfirmationModal
size="small"
- loading={isActivating}
+ loading={isCheckingRecord || isActivating}
visible={isActivateConfirmModalVisible}
header={
<div>
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainConfig.utils.ts b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainConfig.utils.ts
deleted file mode 100644
index ed7343de2b7..00000000000
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainConfig.utils.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-// [Joshen] Should tally with https://github.com/supabase/cli/blob/63790a1bd43bee06f82c4f510e709925526a4daa/internal/utils/api.go#L98
-export const verifyCNAME = async (domain: string): Promise<boolean> => {
- const res = await fetch(`https://1.1.1.1/dns-query?name=${domain}&type=CNAME`, {
- method: 'GET',
- headers: { accept: 'application/dns-json' },
- })
- const verification = await res.json()
- return verification.Status === 0
-}
diff --git a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
index 45d294679bf..7e6816f3092 100644
--- a/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
+++ b/studio/components/interfaces/Settings/General/CustomDomainConfig/CustomDomainsConfigureHostname.tsx
@@ -14,9 +14,9 @@ import {
FormSectionLabel,
} from 'components/ui/Forms'
import { useProjectApiQuery } from 'data/config/project-api-query'
+import { useCheckCNAMERecordMutation } from 'data/custom-domains/check-cname-mutation'
import { useCustomDomainCreateMutation } from 'data/custom-domains/custom-domains-create-mutation'
import { useCheckPermissions, useStore } from 'hooks'
-import { verifyCNAME } from './CustomDomainConfig.utils'
const schema = yup.object({
domain: yup.string().required('A value for your custom domain is required'),
@@ -27,6 +27,14 @@ const CustomDomainsConfigureHostname = () => {
const { ref } = useParams()
const { project } = useProjectContext()
+ const { mutate: checkCNAMERecord, isLoading: isCheckingRecord } = useCheckCNAMERecordMutation({
+ onError: (error, variables) => {
+ return ui.setNotification({
+ category: 'error',
+ message: `Your CNAME record for ${variables.domain} cannot be found - if you've just added the CNAME record, do check back in a bit.`,
+ })
+ },
+ })
const { mutate: createCustomDomain, isLoading: isCreating } = useCustomDomainCreateMutation()
const { data: settings } = useProjectApiQuery({ projectRef: ref })
@@ -41,15 +49,14 @@ const CustomDomainsConfigureHostname = () => {
const onCreateCustomDomain = async (values: yup.InferType<typeof schema>) => {
if (!ref) return console.error('Project ref is required')
- const cnameVerified = await verifyCNAME(values.domain)
- if (!cnameVerified) {
- return ui.setNotification({
- category: 'error',
- message: `Your CNAME record for ${values.domain} cannot be found - if you've just added the CNAME record, do check back in a bit.`,
- })
- }
-
- createCustomDomain({ projectRef: ref, customDomain: values.domain })
+ checkCNAMERecord(
+ { domain: values.domain },
+ {
+ onSuccess: () => {
+ createCustomDomain({ projectRef: ref, customDomain: values.domain })
+ },
+ }
+ )
}
return (
@@ -70,7 +77,7 @@ const CustomDomainsConfigureHostname = () => {
<div className="flex py-4 px-8">
<FormActions
form={FORM_ID}
- isSubmitting={isCreating}
+ isSubmitting={isCheckingRecord || isCreating}
submitText="Add"
hasChanges={hasChanges}
handleReset={handleReset}
diff --git a/studio/components/interfaces/SignIn/SignInMfaForm.tsx b/studio/components/interfaces/SignIn/SignInMfaForm.tsx
index 93f3566cd73..fd51788e8f1 100644
--- a/studio/components/interfaces/SignIn/SignInMfaForm.tsx
+++ b/studio/components/interfaces/SignIn/SignInMfaForm.tsx
@@ -29,7 +29,6 @@ const SignInMfaForm = () => {
error: factorsError,
isError: isErrorFactors,
isSuccess: isSuccessFactors,
- isInitialLoading: isInitialLoadingFactors,
isLoading: isLoadingFactors,
} = useMfaListFactorsQuery()
const {
diff --git a/studio/data/custom-domains/check-cname-mutation.ts b/studio/data/custom-domains/check-cname-mutation.ts
new file mode 100644
index 00000000000..04c4f67a8fe
--- /dev/null
+++ b/studio/data/custom-domains/check-cname-mutation.ts
@@ -0,0 +1,60 @@
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
+import toast from 'react-hot-toast'
+
+import { ResponseError } from 'types'
+
+export type CheckCNAMERecordVariables = {
+ domain: string
+}
+
+export type CheckCNAMERecordResponse = {
+ Status: number
+ TC: boolean
+ RD: boolean
+ RA: boolean
+ AD: boolean
+ CD: boolean
+ Question: { name: string; type: number }[]
+ Answer?: { name: string; type: number; TTL: number; data: string }[]
+ Authority: { name: string; type: number; TTL: number; data: string }[]
+}
+
+// [Joshen] Should tally with https://github.com/supabase/cli/blob/63790a1bd43bee06f82c4f510e709925526a4daa/internal/utils/api.go#L98
+export async function checkCNAMERecord({ domain }: CheckCNAMERecordVariables) {
+ const res = await fetch(`https://1.1.1.1/dns-query?name=${domain}&type=CNAME`, {
+ method: 'GET',
+ headers: { accept: 'application/dns-json' },
+ })
+ const verification = (await res.json()) as CheckCNAMERecordResponse
+
+ if (verification.Answer === undefined) return false
+ return verification.Answer.some((x) => x.type === 5)
+}
+
+type CheckCNAMERecordData = Awaited<ReturnType<typeof checkCNAMERecord>>
+
+export const useCheckCNAMERecordMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<CheckCNAMERecordData, ResponseError, CheckCNAMERecordVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<CheckCNAMERecordData, ResponseError, CheckCNAMERecordVariables>(
+ (vars) => checkCNAMERecord(vars),
+ {
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to check CNAME record: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}