Files
Danny White d7ead91b9f fix(studio): separate account email and password management (#53433)
## Problem

Account Security groups an email address and its password into one
identity row. The disabled unlink control suggests password removal is
temporarily unavailable, even though passwords can be changed but not
removed.

## Solution

Separate email address management from password management:

```text
Sign-in methods
  Email address    [edit]
  Password         Configured [edit]
  GitHub / ChatGPT [unlink]
```

- Accounts without a password get a Password row with an “Add” action
that opens the existing setup form
- Single-provider accounts retain the recovery warning, with the row now
owning the only “Add” action
- Existing MFA and recovery-code action priority is preserved
- Use distinct icons for password, SSO and generic providers, and
consistent edit/unlink controls (which resolves DEPR-715)

| After: GitHub Only | After: Password |
| --- | --- |
| <img width="1510" height="1554" alt="CleanShot 2026-11-16 at 13 29
21@2x"
src="https://github.com/user-attachments/assets/f71b86a1-81aa-4b4f-a64f-174c3550db73"
/> | <img width="1498" height="1358" alt="CleanShot 2026-11-16 at 13 29
57@2x"
src="https://github.com/user-attachments/assets/e36f2ed4-b580-4126-87b7-a655f4333427"
/> |


| After: Icon Refinment |
| --- |
| <img width="1570" height="846" alt="64227"
src="https://github.com/user-attachments/assets/556ff03c-a806-4c27-9ba8-b74992f4fe0d"
/> |
| <img width="1454" height="392" alt="CleanShot 2026-11-16 at 13 42
02@2x"
src="https://github.com/user-attachments/assets/c4215446-13aa-40d2-9e26-c7ae0f071395"
/> |

## Review instructions

Open [Account → Security
locally](http://localhost:8082/account/security) with Studio running
(`pnpm dev:studio`). Vercel marked the Studio deployment as ignored for
this PR, so no deploy preview is available.

1. **Account with a password:** check that Email address and Password
are separate rows. Password shows “Configured”. Hover or focus the
pencil buttons to see “Update email address” and “Change password”. The
password pencil opens the existing change-password page. Neither row
offers unlink.
2. **OAuth-only account, with `enableAccountPassword` enabled:** check
the Password row has an **Add** button. With one OAuth provider, the
warning appears above the rows but has no duplicate button. Click Add:
the existing password form shows the account email. Cancel and reopen
it. If using a disposable account, save a valid password and check that
the row changes to “Configured”.
3. **Linked external identity:** check that Unlink uses the same button
shape as the edit controls and still opens the confirmation. Cancel it.
4. **SSO account, if available:** check the briefcase icon and “Managed
by your organization”. Password setup must not be offered.
5. Repeat the row and action checks at phone width. Labels, addresses
and actions should remain visible.
2026-11-18 09:58:49 +11:00

172 lines
5.5 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { EyeIcon, EyeOffIcon, KeyRoundIcon } from 'lucide-react'
import { parseAsString, useQueryState } from 'nuqs'
import { useState } from 'react'
import { useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
CardContent,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogSection,
DialogTitle,
Form,
FormControl,
FormField,
Input,
} from 'ui'
import { Input as InputWithActions } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { z } from 'zod'
import PasswordConditionsHelper from '@/components/interfaces/SignIn/PasswordConditionsHelper'
import { useSetPasswordMutation } from '@/data/profile/profile-set-password-mutation'
import { passwordValidation } from '@/lib/password-validation'
const FORM_ID = 'add-password-form'
const EMAIL_INPUT_ID = 'add-password-email'
const FormSchema = z.object({ password: passwordValidation })
type FormValues = z.infer<typeof FormSchema>
const defaultValues: FormValues = { password: '' }
/**
* Offers a user without an email identity (OAuth-only account) a way to create one by setting a
* password.
*
* Auth creates the email identity for the user's current email when the password is
* saved.
*/
export const AddPasswordRow = ({
email,
isPrimary = false,
}: {
email: string
isPrimary?: boolean
}) => {
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [securityAction, setSecurityAction] = useQueryState('securityAction', parseAsString)
const isOpen = isDialogOpen || securityAction === 'set-password'
const handleOpenChange = (open: boolean) => {
setIsDialogOpen(open)
if (!open && securityAction === 'set-password') void setSecurityAction(null)
}
return (
<>
<CardContent
role="group"
aria-label="Password"
className="flex flex-col items-start gap-4 py-4 sm:flex-row sm:items-center sm:justify-between"
>
<div className="flex min-w-0 gap-x-4">
<div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-selection">
<KeyRoundIcon size={18} strokeWidth={2} className="text-foreground-lighter" />
</div>
<div className="min-w-0">
<p className="text-sm">Password</p>
<p className="text-sm text-foreground-lighter [overflow-wrap:anywhere]">
Add a password to sign in with {email}
</p>
</div>
</div>
<Button
variant={isPrimary ? 'primary' : 'default'}
aria-label="Add password"
onClick={() => handleOpenChange(true)}
>
Add
</Button>
</CardContent>
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
<DialogContent>
<DialogHeader className="border-b">
<DialogTitle>Add password</DialogTitle>
</DialogHeader>
<AddPasswordForm email={email} onClose={() => handleOpenChange(false)} />
</DialogContent>
</Dialog>
</>
)
}
const AddPasswordForm = ({ email, onClose }: { email: string; onClose: () => void }) => {
const [passwordHidden, setPasswordHidden] = useState(true)
const form = useForm<FormValues>({
resolver: zodResolver(FormSchema),
defaultValues,
mode: 'onChange',
})
const password = useWatch({ control: form.control, name: 'password' })
const { mutate: setPassword, isPending } = useSetPasswordMutation({
onSuccess: () => {
toast.success('Password added successfully')
onClose()
},
})
const onSubmit = (values: FormValues) => setPassword({ password: values.password })
return (
<Form {...form}>
<form id={FORM_ID} onSubmit={form.handleSubmit(onSubmit)}>
<DialogSection className="flex flex-col gap-y-4">
<FormItemLayout
isReactForm={false}
id={EMAIL_INPUT_ID}
label="Email"
description="Adding a password lets you sign in with this email address"
>
<Input id={EMAIL_INPUT_ID} disabled value={email} />
</FormItemLayout>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItemLayout label="Password">
<FormControl>
<InputWithActions
id="password"
type={passwordHidden ? 'password' : 'text'}
placeholder="&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;"
disabled={isPending}
autoComplete="new-password"
actions={
<Button
icon={passwordHidden ? <EyeIcon /> : <EyeOffIcon />}
aria-label={passwordHidden ? 'Show password' : 'Hide Password'}
className="w-7"
onClick={() => setPasswordHidden((prev) => !prev)}
/>
}
{...field}
/>
</FormControl>
</FormItemLayout>
)}
/>
<PasswordConditionsHelper password={password} />
</DialogSection>
<DialogFooter>
<Button disabled={isPending} onClick={onClose}>
Cancel
</Button>
<Button variant="primary" type="submit" loading={isPending} disabled={isPending}>
Add password
</Button>
</DialogFooter>
</form>
</Form>
)
}