Fix turnstile

This commit is contained in:
2026-10-15 15:53:32 +02:00
parent 7dc93ac5ed
commit 70b676660d
80 changed files with 1136 additions and 902 deletions
@@ -1,41 +1,69 @@
'use client'
import type { TextFieldClientComponent } from 'payload'
import { useField, FieldLabel, FieldDescription } from '@payloadcms/ui'
import { useState } from 'react'
import type { TextFieldClientComponent } from 'payload'
export const MaskedField: TextFieldClientComponent = (props: any) => {
const { field, path, value: propValue, setValue: propSetValue, onChange: propOnChange } = props || {}
const [internalValue, setInternalValue] = useState(propValue ?? '')
/**
* Masked text input for sensitive values (SMTP password, R2 secret key,
* Turnstile secret key, etc.).
*
* Renders the value as a password field (••••) so secrets aren't shown in
* plaintext on screen — protecting against shoulder-surfing and accidental
* exposure when the panel is open. The value is still stored and returned
* normally (UI masking, NOT hashing or encryption), because the plugin must
* read it back to authenticate with SMTP/R2/Turnstile. A toggle reveals the
* value when an authorised editor genuinely needs to check it.
*
* Uses a raw <input> driven by useField rather than @payloadcms/ui's TextInput,
* because TextInput doesn't expose inputProps, so there's no way to switch its
* inner input to type="password".
*
* Assigned via a field's admin.components.Field. Works on any `text` field.
*/
export const MaskedField: TextFieldClientComponent = ({ field, path }) => {
const { value, setValue } = useField<string>({ path })
const [revealed, setRevealed] = useState(false)
const label = typeof field?.label === 'string' ? field.label : (field?.name ?? path)
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newVal = e.target.value
setInternalValue(newVal)
if (typeof propSetValue === 'function') {
propSetValue(newVal)
}
if (typeof propOnChange === 'function') {
propOnChange(e)
}
}
const currentValue = propValue !== undefined ? propValue : internalValue
const label =
typeof field?.label === 'string' ? field.label : (field?.name ?? path)
const description = field?.admin?.description
return (
<div className="field-type text">
{label && <label className="field-label">{label}</label>}
<div style={{ display: 'flex', gap: '.5rem' }}>
<div className="field-type text masked-field">
<FieldLabel label={label} htmlFor={`field-${path}`} />
<div className="field-type__wrap" style={{ display: 'flex', gap: '.5rem', alignItems: 'center' }}>
<input
autoComplete="off"
onChange={handleChange}
style={{ flex: 1 }}
id={`field-${path}`}
type={revealed ? 'text' : 'password'}
value={currentValue ?? ''}
autoComplete="off"
className="field-type__input"
value={value ?? ''}
onChange={(e) => setValue(e.target.value)}
style={{ flex: 1 }}
/>
<button onClick={() => setRevealed((r) => !r)} type="button">
<button
type="button"
onClick={() => setRevealed((r) => !r)}
style={{
background: 'none',
border: 'none',
padding: '.25rem .5rem',
cursor: 'pointer',
font: 'inherit',
textDecoration: 'underline',
opacity: 0.75,
whiteSpace: 'nowrap',
}}
>
{revealed ? 'Hide' : 'Reveal'}
</button>
</div>
{description && typeof description === 'string' ? (
<FieldDescription path={path} description={description} />
) : null}
</div>
)
}