Fix email form server check and return error code if existing

This commit is contained in:
2022-09-14 22:35:04 +02:00
parent c7ddfc7adf
commit 91c28cf1e3
2 changed files with 5 additions and 4 deletions

View File

@@ -32,7 +32,7 @@
const formData = new FormData(target) const formData = new FormData(target)
const email = String(formData.get('email')) const email = String(formData.get('email'))
if (email && email.match(/^([\w.-])+@([\w.-])+\.([a-zA-Z])+/)) { if (email && email.match(/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/)) {
const req = await fetch('/api/newsletter', { const req = await fetch('/api/newsletter', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'text/plain' }, headers: { 'Content-Type': 'text/plain' },
@@ -94,7 +94,8 @@
<div class="newsletter-form__message shadow-small" <div class="newsletter-form__message shadow-small"
class:is-error={formStatus !== 'PENDING'} class:is-error={formStatus !== 'PENDING'}
class:is-success={formStatus === 'PENDING'} class:is-success={formStatus === 'PENDING'}
transition:fly={{ y: 8, easing: quartOut, duration: 600, delay: formStatus !== 'PENDING' ? 100 : 600 }} in:fly={{ y: 8, easing: quartOut, duration: 600, delay: 600 }}
out:fly={{ y: 8, easing: quartOut, duration: 600 }}
> >
<p class="text-xsmall">{formMessages[formStatus]}</p> <p class="text-xsmall">{formMessages[formStatus]}</p>
</div> </div>

View File

@@ -18,9 +18,9 @@ export const POST: RequestHandler = async ({ request }) => {
}) })
const res = await req.json() const res = await req.json()
if (res && res.email_address && res.status === 'PENDING') { if (res) {
return new Response(JSON.stringify({ return new Response(JSON.stringify({
code: 'PENDING' code: res.error ? res.error.code : res.status
})) }))
} }
} }