Desktop register: validate email format client-side and disable submit to avoid 400

This commit is contained in:
Esdras Renan 2025-10-19 02:25:12 -03:00
parent 2c7c22d70b
commit 2a8fb4330c

View file

@ -207,6 +207,8 @@ function App() {
const autoLaunchRef = useRef(false) const autoLaunchRef = useRef(false)
const autoUpdateRef = useRef(false) const autoUpdateRef = useRef(false)
const logoFallbackRef = useRef(false) const logoFallbackRef = useRef(false)
const emailRegex = useRef(/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i)
const isEmailValid = useMemo(() => emailRegex.current.test(collabEmail.trim()), [collabEmail])
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@ -449,6 +451,10 @@ function App() {
setError("Informe o e-mail do colaborador vinculado a esta máquina.") setError("Informe o e-mail do colaborador vinculado a esta máquina.")
return return
} }
if (!emailRegex.current.test(normalizedEmail)) {
setError("Informe um e-mail válido (ex.: nome@empresa.com)")
return
}
const normalizedName = collabName.trim() const normalizedName = collabName.trim()
if (!normalizedName) { if (!normalizedName) {
setError("Informe o nome completo do colaborador.") setError("Informe o nome completo do colaborador.")
@ -837,10 +843,14 @@ function App() {
</label> </label>
<input <input
className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm" className="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"
type="email"
placeholder="colaborador@empresa.com" placeholder="colaborador@empresa.com"
value={collabEmail} value={collabEmail}
onChange={(e) => setCollabEmail(e.target.value)} onChange={(e) => setCollabEmail(e.target.value)}
/> />
{collabEmail && !isEmailValid ? (
<p className="text-xs text-rose-600">Informe um e-mail válido (ex.: nome@empresa.com)</p>
) : null}
</div> </div>
<div className="grid gap-2"> <div className="grid gap-2">
<label className="text-sm font-medium"> <label className="text-sm font-medium">
@ -874,7 +884,7 @@ function App() {
</div> </div>
) : null} ) : null}
<div className="mt-2 flex gap-2"> <div className="mt-2 flex gap-2">
<button disabled={busy || !validatedCompany} onClick={register} className="rounded-lg border border-black bg-black px-3 py-2 text-sm font-semibold text-white hover:bg-black/90 disabled:opacity-60">Registrar máquina</button> <button disabled={busy || !validatedCompany || !isEmailValid || !collabName.trim() || provisioningCode.trim().length < 32} onClick={register} className="rounded-lg border border-black bg-black px-3 py-2 text-sm font-semibold text-white hover:bg-black/90 disabled:opacity-60">Registrar máquina</button>
</div> </div>
</div> </div>
) : ( ) : (