Files
supabase/apps/studio/components/interfaces/Organization/SSO/SSOMetadata.tsx
32b5d8c244 feat: enable Self-Service Single Sign-On (SSO) for orgs (#36732)
* Start

* Add join org logic

* Regenerate the API types and add RQ hooks.

* Various fixes to the UI for SSO config.

* Refactor the components to use RHF.

* Fix the loading/error states in the main file.

* fix: minor changes to SSO UI

* Expanded default attribute mapping preset for `Okta` to include `user_name`, `first_name`, and `last_name` to match our docs.
* Normalized role casing in the "Join Organization on Signup" dropdown to match the roles expected by the backend (`Owner`, `Administrator`, `Developer`).
* Added the role (`Read-only`) to the selectable roles for auto-join.
* Call update mutation when an `ssoConfig` has been retrieved.
* Treats `404` as a valid "create" flow state rather than an error.
* Conditionally renders the SSO config form when config is successfully loaded *or* when the provider is not found, allowing users to onboard from scratch and disable the provider.

* chore: prettier

* feat: add a button linking to SSO setup docs

* Revert "feat: add a button linking to SSO setup docs"

This will be included in a separate docs pr.

This reverts commit 0b616fdd2e.

* General clean up

* Nit copy

* Add empty state for SSO

* Smol change

* One last tweak

---------

Co-authored-by: Chris Stockton <180184+cstockton@users.noreply.github.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
Co-authored-by: Chris Stockton <chris.stockton@supabase.io>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-08-05 17:03:30 -07:00

131 lines
4.0 KiB
TypeScript

import { Upload } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import { useForm } from 'react-hook-form'
import {
Button,
FormControl_Shadcn_,
FormField_Shadcn_,
FormItem_Shadcn_,
FormMessage_Shadcn_,
Input_Shadcn_,
Tabs_Shadcn_,
TabsContent_Shadcn_,
TabsList_Shadcn_,
TabsTrigger_Shadcn_,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { SSOConfigFormSchema } from './SSOConfig'
export const SSOMetadata = ({
form,
}: {
form: ReturnType<typeof useForm<SSOConfigFormSchema>>
}) => {
const fileInputRef = useRef<HTMLInputElement>(null)
const [fileName, setFileName] = useState<string | null>(null)
const [tab, setTab] = useState<'url' | 'file'>('url')
useEffect(() => {
if (form.getValues('metadataXmlFile')) {
setTab('file')
} else if (form.getValues('metadataXmlUrl')) {
setTab('url')
}
}, [form])
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
form.clearErrors('metadataXmlFile')
const file = e.target.files?.[0]
if (!file) return
if (!file.name.endsWith('.xml')) {
form.setError('metadataXmlFile', {
type: 'manual',
message: 'Please upload a valid .xml file',
})
return
}
try {
const text = await file.text()
form.setValue('metadataXmlFile', text, { shouldDirty: true })
setFileName(file.name)
} catch (err) {
form.setError('metadataXmlFile', {
type: 'manual',
message: 'Failed to read file',
})
}
}
return (
<FormItemLayout
label="Metadata"
layout="flex-row-reverse"
description="Provide a link to your metadata .xml file or upload one"
className="gap-1"
>
<div className=" w-96">
<Tabs_Shadcn_
value={tab}
onValueChange={(value: string) => setTab(value as 'url' | 'file')}
className="max-w-2xl"
>
<TabsList_Shadcn_ className="mx-auto gap-5 w-auto">
<TabsTrigger_Shadcn_ className=" " value="url">
URL
</TabsTrigger_Shadcn_>
<TabsTrigger_Shadcn_ className=" " value="file">
Upload file
</TabsTrigger_Shadcn_>
</TabsList_Shadcn_>
<TabsContent_Shadcn_ value="url">
<FormField_Shadcn_
name="metadataXmlUrl"
render={({ field }) => (
<FormItem_Shadcn_>
<FormControl_Shadcn_>
<Input_Shadcn_
{...field}
placeholder="https://example.com/metadata.xml"
autoComplete="off"
/>
</FormControl_Shadcn_>
<FormMessage_Shadcn_ />
</FormItem_Shadcn_>
)}
/>
</TabsContent_Shadcn_>
<TabsContent_Shadcn_ value="file">
<FormField_Shadcn_
name="metadataXmlUrl"
render={() => (
<div className="flex flex-col gap-2 max-w-md">
<div className="flex items-center gap-2">
<input
ref={fileInputRef}
type="file"
accept=".xml"
className="hidden"
onChange={handleFileChange}
/>
<Button
type="default"
size="small"
icon={<Upload className="w-4 h-4" />}
onClick={() => fileInputRef.current?.click()}
>
Upload XML
</Button>
{fileName && <span className="text-xs text-foreground-light">{fileName}</span>}
</div>
<FormMessage_Shadcn_ />
</div>
)}
/>
</TabsContent_Shadcn_>
</Tabs_Shadcn_>
</div>
</FormItemLayout>
)
}