mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 5 of a 6-PR stack migrating all `next/link` imports in `apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known external URLs) and removing `compat/next/link.tsx`. | Part | PR | Scope | | --- | --- | --- | | 1 | #54304 | `data/prefetchers/PrefetchableLink.tsx` and `components/ui/**` | | 2 | #54305 | `components/layouts/**` and top-level shell components | | 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor, BranchManagement, Branching, Functions, Realtime | | 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues, Linter, Reports, QueryPerformance, QuerySources, ConfigDrift, DiskManagement, Compute | | **5** | **#54308 ← this PR** | **Remaining `components/interfaces/**` (Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.)** | | 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and update basePath tests/docs | Migrates the remaining `components/interfaces/**` directories off `next/link`, leaving zero `next/link` component imports in `apps/studio`. **Changed:** - Replaced `next/link` across all 73 remaining interface files (`Organization`, `OrganizationInvite`, `Billing`, `Auth`, `Support`, `ProjectHome`, `Home`, `Project`, `ProjectCreation`, `ProjectAPIDocs`, `ApiAuthorization`, `App`, `ConnectSheet`, `SignIn`, `APIKeys`, `Account`, `CliLogin`, `Docs`, `JwtSecrets`, `Platform`) - Converted the sole remaining `UrlObject` link (`<Link href={{ pathname: '/sign-in-sso', query }}>`) in `SignInSSOForm.tsx` to `<Link to="/sign-in-sso" search={query}>` - Migrated `SupportLink.tsx` onto `DynamicLink` (`createSupportFormUrl(...)`) - Standardized external `target="_blank"` anchors with `rel="noreferrer noopener"`, switched `OverviewMetrics.tsx` observability link to typed `<Link to="/project/$ref/observability/auth">`, and adopted `InlineLink` / `InlineLinkClassName` on inline paragraph links per review feedback Addresses [FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link) ## To test - [ ] Check Organization (billing, usage, team, documents, AWS Marketplace), Auth (providers, templates, rate limits, users, OAuth apps), Support form/links, Project Home, Connect sheet, and Sign-In/SSO links - [ ] Verify `SignInWithSSOButton` preserves query params when navigating to `/sign-in-sso` - [ ] Verify `SupportLink` pre-populates support form query parameters cleanly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
204 lines
7.8 KiB
TypeScript
204 lines
7.8 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { Link } from '@tanstack/react-router'
|
|
import { useParams } from 'common'
|
|
import { useEffect } from 'react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { toast } from 'sonner'
|
|
import { Button, Card, CardContent, CardFooter, Form, FormControl, FormField, Switch } from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import { PageContainer } from 'ui-patterns/PageContainer'
|
|
import { PageSection } from 'ui-patterns/PageSection'
|
|
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
|
|
import { z } from 'zod'
|
|
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { NoPermission } from '@/components/ui/NoPermission'
|
|
import { UpgradeToPro } from '@/components/ui/UpgradeToPro'
|
|
import { useOrganizationMembersQuery } from '@/data/organizations/organization-members-query'
|
|
import { useOrganizationMfaToggleMutation } from '@/data/organizations/organization-mfa-mutation'
|
|
import { useOrganizationMfaQuery } from '@/data/organizations/organization-mfa-query'
|
|
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useProfile } from '@/lib/profile'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
const schema = z.object({
|
|
enforceMfa: z.boolean(),
|
|
})
|
|
|
|
export const SecuritySettings = () => {
|
|
const { slug } = useParams()
|
|
const { profile } = useProfile()
|
|
const {
|
|
data: members,
|
|
error: membersError,
|
|
isPending: isLoadingMembers,
|
|
isError: isMembersError,
|
|
isSuccess: isSuccessMembers,
|
|
} = useOrganizationMembersQuery({ slug })
|
|
|
|
const { can: canReadMfaConfig, isLoading: isLoadingPermissions } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.ORGANIZATION.CONFIG_READ
|
|
)
|
|
const { can: canUpdateMfaConfig } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.ORGANIZATION.CONFIG_WRITE
|
|
)
|
|
const track = useTrack()
|
|
|
|
const { hasAccess: hasAccessToEnforceMfa, isLoading: isLoadingEntitlement } =
|
|
useCheckEntitlements('security.enforce_mfa')
|
|
|
|
const {
|
|
data: mfaConfig,
|
|
error: mfaError,
|
|
isPending: isLoadingMfa,
|
|
isError: isErrorMfa,
|
|
isSuccess: isSuccessMfa,
|
|
} = useOrganizationMfaQuery({ slug }, { enabled: hasAccessToEnforceMfa && canReadMfaConfig })
|
|
|
|
const { mutate: toggleMfa, isPending: isUpdatingMfa } = useOrganizationMfaToggleMutation({
|
|
onError: (error) => {
|
|
toast.error(`Failed to update MFA enforcement: ${error.message}`)
|
|
if (mfaConfig !== undefined) form.reset({ enforceMfa: mfaConfig })
|
|
},
|
|
onSuccess: (data) => {
|
|
toast.success('Successfully updated organization MFA settings')
|
|
track('organization_mfa_enforcement_updated', { mfaEnforced: data.enforced })
|
|
},
|
|
})
|
|
|
|
const form = useForm<z.infer<typeof schema>>({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: {
|
|
enforceMfa: false,
|
|
},
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (mfaConfig !== undefined) {
|
|
form.reset({ enforceMfa: mfaConfig })
|
|
}
|
|
}, [mfaConfig, form])
|
|
|
|
const hasMFAEnabled =
|
|
members?.find((member) => member.primary_email == profile?.primary_email)?.mfa_enabled ?? false
|
|
|
|
const requiresPersonalMfa = isSuccessMembers && canUpdateMfaConfig && !hasMFAEnabled
|
|
|
|
const isLoadingMfaEnforcementSettings =
|
|
isLoadingMfa || isLoadingPermissions || isLoadingEntitlement || isLoadingMembers
|
|
const hasMfaConfigError = (isErrorMfa || Boolean(mfaError)) && hasAccessToEnforceMfa
|
|
const canShowMfaEnforcementForm =
|
|
isSuccessMfa && hasAccessToEnforceMfa && isSuccessMembers && !requiresPersonalMfa
|
|
const isMfaEnforcementSwitchDisabled =
|
|
!hasAccessToEnforceMfa || !canUpdateMfaConfig || isUpdatingMfa
|
|
const isSaveMfaEnforcementDisabled =
|
|
isMfaEnforcementSwitchDisabled || isLoadingMfa || !form.formState.isDirty
|
|
|
|
const onSubmit = (values: { enforceMfa: boolean }) => {
|
|
if (!slug || !hasAccessToEnforceMfa) return
|
|
toggleMfa({ slug, setEnforced: values.enforceMfa })
|
|
}
|
|
|
|
return (
|
|
<PageContainer size="small">
|
|
<PageSection>
|
|
{!hasAccessToEnforceMfa && !isLoadingEntitlement ? (
|
|
<UpgradeToPro
|
|
source="organizationMfa"
|
|
primaryText="Organization MFA enforcement is not available on Free Plan"
|
|
secondaryText="Upgrade to Pro or above to enforce MFA requirements for your organization."
|
|
featureProposition="enforce MFA requirements"
|
|
/>
|
|
) : (
|
|
<>
|
|
{isLoadingMfaEnforcementSettings ? (
|
|
<Card>
|
|
<CardContent>
|
|
<GenericSkeletonLoader />
|
|
</CardContent>
|
|
</Card>
|
|
) : !canReadMfaConfig ? (
|
|
<NoPermission resourceText="view organization security settings" />
|
|
) : (
|
|
requiresPersonalMfa && (
|
|
<Admonition
|
|
type="note"
|
|
layout="horizontal"
|
|
title="Enable MFA on your account first"
|
|
description="You need to set up multi-factor authentication (MFA) on your own account before you can enforce it on your organization."
|
|
actions={
|
|
<Button asChild>
|
|
<Link to="/account/security">Set up MFA</Link>
|
|
</Button>
|
|
}
|
|
/>
|
|
)
|
|
)}
|
|
|
|
{isMembersError && (
|
|
<AlertError error={membersError} subject="Failed to retrieve organization members" />
|
|
)}
|
|
|
|
{hasMfaConfigError && (
|
|
<AlertError error={mfaError} subject="Failed to retrieve MFA enforcement status" />
|
|
)}
|
|
|
|
{canShowMfaEnforcementForm && (
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
<Card>
|
|
<CardContent>
|
|
<FormField
|
|
control={form.control}
|
|
name="enforceMfa"
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
layout="flex-row-reverse"
|
|
className="justify-between"
|
|
label="Require MFA to access organization"
|
|
description="Team members must have MFA enabled and a valid MFA session to access the organization and any projects."
|
|
>
|
|
<FormControl>
|
|
<Switch
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
disabled={isMfaEnforcementSwitchDisabled}
|
|
/>
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</CardContent>
|
|
<CardFooter className="justify-end space-x-2">
|
|
{form.formState.isDirty && (
|
|
<Button
|
|
disabled={isLoadingMfa || isUpdatingMfa}
|
|
onClick={() =>
|
|
form.reset({ enforceMfa: hasAccessToEnforceMfa ? mfaConfig : false })
|
|
}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
)}
|
|
<Button
|
|
variant="primary"
|
|
type="submit"
|
|
disabled={isSaveMfaEnforcementDisabled}
|
|
loading={isUpdatingMfa}
|
|
>
|
|
Save
|
|
</Button>
|
|
</CardFooter>
|
|
</Card>
|
|
</form>
|
|
</Form>
|
|
)}
|
|
</>
|
|
)}
|
|
</PageSection>
|
|
</PageContainer>
|
|
)
|
|
}
|