Fix turnstile
This commit is contained in:
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,7 @@ import { useState } from 'react'
|
||||
export const TestEmailButton = () => {
|
||||
const [to, setTo] = useState('')
|
||||
const [status, setStatus] = useState<
|
||||
| { kind: 'error'; msg: string }
|
||||
| { kind: 'idle' }
|
||||
| { kind: 'ok'; msg: string }
|
||||
| { kind: 'sending' }
|
||||
{ kind: 'idle' } | { kind: 'sending' } | { kind: 'ok'; msg: string } | { kind: 'error'; msg: string }
|
||||
>({ kind: 'idle' })
|
||||
|
||||
const send = async () => {
|
||||
@@ -28,10 +25,10 @@ export const TestEmailButton = () => {
|
||||
setStatus({ kind: 'sending' })
|
||||
try {
|
||||
const res = await fetch('/api/ipal/test-email', {
|
||||
body: JSON.stringify({ to: to.trim() }),
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ to: to.trim() }),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (data.ok) {
|
||||
@@ -47,23 +44,23 @@ export const TestEmailButton = () => {
|
||||
return (
|
||||
<div className="field-type" style={{ marginTop: '1rem' }}>
|
||||
<label className="field-label">Send a test email</label>
|
||||
<p style={{ fontSize: '.85rem', marginTop: 0, opacity: 0.7 }}>
|
||||
<p style={{ opacity: 0.7, fontSize: '.85rem', marginTop: 0 }}>
|
||||
Sends through the transport selected above. Save your changes first.
|
||||
</p>
|
||||
<div style={{ alignItems: 'center', display: 'flex', flexWrap: 'wrap', gap: '.5rem' }}>
|
||||
<div style={{ display: 'flex', gap: '.5rem', alignItems: 'center', flexWrap: 'wrap' }}>
|
||||
<input
|
||||
onChange={(e) => setTo(e.target.value)}
|
||||
placeholder="[email protected]"
|
||||
style={{ flex: 1, minWidth: '220px' }}
|
||||
type="email"
|
||||
placeholder="[email protected]"
|
||||
value={to}
|
||||
onChange={(e) => setTo(e.target.value)}
|
||||
style={{ flex: 1, minWidth: '220px' }}
|
||||
/>
|
||||
<button
|
||||
className="btn btn--style-secondary"
|
||||
disabled={status.kind === 'sending'}
|
||||
onClick={send}
|
||||
style={{ whiteSpace: 'nowrap' }}
|
||||
type="button"
|
||||
onClick={send}
|
||||
disabled={status.kind === 'sending'}
|
||||
className="btn btn--style-secondary"
|
||||
style={{ whiteSpace: 'nowrap' }}
|
||||
>
|
||||
{status.kind === 'sending' ? 'Sending…' : 'Send test'}
|
||||
</button>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { Field } from 'payload'
|
||||
|
||||
import { buildIntegrationStatusBanner, buildIntegrationStatusField } from '../../../modules/integrations/status.js'
|
||||
|
||||
/**
|
||||
* SMTP transport settings for outbound email.
|
||||
*
|
||||
@@ -8,6 +10,8 @@ import type { Field } from 'payload'
|
||||
* panel while remaining inaccessible to anonymous API requests.
|
||||
*/
|
||||
export const smtpFields: Field[] = [
|
||||
// Status written by mailAdapter on every send (throttled) — banner on this tab.
|
||||
buildIntegrationStatusBanner('emailStatus', 'Email'),
|
||||
{
|
||||
name: 'emailTransport',
|
||||
type: 'select',
|
||||
@@ -87,4 +91,5 @@ export const smtpFields: Field[] = [
|
||||
},
|
||||
},
|
||||
},
|
||||
buildIntegrationStatusField('emailStatus'),
|
||||
]
|
||||
|
||||
@@ -51,13 +51,15 @@ export function buildSiteIntegrations({
|
||||
],
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
// Keys edited in the panel → clear the old Turnstile status, so a stale
|
||||
// Keys edited in the panel → clear that integration's old status, so a stale
|
||||
// "not working" banner doesn't linger until the next form submission.
|
||||
({ context, data, originalDoc }) => {
|
||||
if (context?.ipalIntegrationStatus || !data) {return data}
|
||||
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
|
||||
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {
|
||||
data.turnstileStatus = { checkedAt: null, code: null, message: null, since: null, state: null }
|
||||
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
|
||||
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {data.turnstileStatus = cleared}
|
||||
if (Object.keys(data).some((k) => (k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
|
||||
data.emailStatus = cleared
|
||||
}
|
||||
return data
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user