Files
ipal-kit/src/globals/SiteIntegrations/components/MaskedField.tsx
T
Radosław SmolińskiandClaude Fable 5.2 1141c749de M1 review: account rules before 2FA, the SMTP password stays with its server, the rest of the findings
M1-1 the first-user route is the plugin's own endpoint in front of Payload's; nothing is read from the path.
M1-2 the account rules are put on the collections before the 2FA package wraps them.
M1-6 the admin rule only where the roles are the plugin's; other staff collections: an account of that collection.
     update / delete get the matching defaults (accounts.adminOnlyWrite); doctor rule staff-accounts-open.
M1-5 access.firstUserAdmin: false with a role below the panel role: the default panel role steps aside, an explicit one is refused.
M1-3 a save that changes the SMTP server, port, user or encryption must send the password again (secrets.reenterOnChange);
     new panel field smtpRequireTls.
M1-4 a restore of a version masks the secrets (maskSecretsOnRestore).
M1-7 a refused origin is logged once with the ways to allow it; default ports are normalised.
Minors: m1 day boundary, m3 e-mail check on reset and bulk update, m4 text of 'all', m5 the mask is never kept.
The account suite runs with 2FA off and on.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 00:39:23 +01:00

91 lines
3.8 KiB
TypeScript

'use client'
import type { TextFieldClientComponent } from 'payload'
import { FieldDescription, FieldLabel, useField, useTranslation } from '@payloadcms/ui'
import { useState } from 'react'
import { ipalT } from '../../../modules/admin-i18n/t.js'
import { SECRET_CLEAR, SECRET_MASK } from '../../../modules/secrets/constants.js'
/**
* Text input for a secret (SMTP password, Turnstile secret key, …).
*
* With a write-only field (modules/secrets/writeOnly.ts — the default for the plugin's own
* secrets) the stored value never reaches the browser: the field arrives as `SECRET_MASK`.
* The input then stays empty and says that a value is set; typing replaces it, leaving it
* alone keeps it, "Remove the stored value" clears it on save.
*
* Without write-only (`secrets: { writeOnly: false }`, or a plain text field of a project) the
* value itself arrives; it is drawn as dots with a Show / Hide toggle, as before.
*
* Uses a raw <input> driven by useField rather than @payloadcms/ui's TextInput, because
* TextInput doesn't expose inputProps, so its inner input cannot be type="password".
*
* Assigned via a field's admin.components.Field. Works on any `text` field.
*/
export const MaskedField: TextFieldClientComponent = ({ field, path }) => {
const { t } = useTranslation()
const { errorMessage, setValue, showError, value } = useField<string>({ path })
const [revealed, setRevealed] = useState(false)
// A string or one string per language — FieldLabel / FieldDescription translate it.
const label = field?.label || field?.name || path
const description = field?.admin?.description
const stored = value === SECRET_MASK // a value is set on the server; this is not it
const removing = value === SECRET_CLEAR
const link = { background: 'none', border: 'none', cursor: 'pointer', font: 'inherit', opacity: 0.75, padding: '.25rem .5rem', textDecoration: 'underline', whiteSpace: 'nowrap' } as const
return (
<div className="field-type text masked-field">
<FieldLabel htmlFor={`field-${path}`} label={label} />
<div className="field-type__wrap" style={{ alignItems: 'center', display: 'flex', gap: '.5rem' }}>
{/* Labelled by <FieldLabel htmlFor> above — the rule can't see through the component. */}
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<input
autoComplete="off"
className="field-type__input"
id={`field-${path}`}
onChange={(e) => setValue(e.target.value)}
placeholder={stored ? '••••••••' : undefined}
style={{ flex: 1 }}
type={revealed ? 'text' : 'password'}
value={stored || removing ? '' : (value ?? '')}
/>
{stored ? (
<button onClick={() => setValue(SECRET_CLEAR)} style={link} type="button">
{ipalT(t as never, 'maskedRemove')}
</button>
) : removing ? (
<button onClick={() => setValue(SECRET_MASK)} style={link} type="button">
{ipalT(t as never, 'maskedKeep')}
</button>
) : (
<button onClick={() => setRevealed((r) => !r)} style={link} type="button">
{ipalT(t as never, revealed ? 'maskedHide' : 'maskedShow')}
</button>
)}
</div>
{showError && errorMessage ? (
<p className="field-description" role="alert" style={{ color: 'var(--theme-error-500)', margin: '.25rem 0 0' }}>
{errorMessage}
</p>
) : null}
{stored || removing ? (
<p className="field-description" role="status" style={{ margin: '.25rem 0 0' }}>
{ipalT(t as never, stored ? 'maskedSet' : 'maskedRemoved')}
</p>
) : null}
{description && typeof description !== 'function' ? (
<FieldDescription description={description} path={path} />
) : null}
</div>
)
}
export default MaskedField