Heim > Web-Frontend > js-Tutorial > Kontaktformular und CAPTCHA-Backend in Open Source Cloud

Kontaktformular und CAPTCHA-Backend in Open Source Cloud

Mary-Kate Olsen
Freigeben: 2024-10-04 22:26:02
Original
642 Leute haben es durchsucht

With an open source backend service for form collection and CAPTCHA handling you can implement a contact form with spam protection without developing your own backend services for this. These backend services are also available in Open Source Cloud so you don't have to host these backend services yourself either.

In this blog I will describe how you can quickly get a contact form where the message is sent to a Slack channel.

Contact form and CAPTCHA backend in Open Source Cloud

Create a Slack Bot

The Slack Bot will be the one posting the message to the channel in Slack. Visit https://api.slack.com/apps/ and create a new app with the permissions to post to the desired Slack channel.

Save the Slack Bot token as this will be used later in this tutorial.

Create form backend service in Open Source Cloud

Login to or signup for an account at Open Source Cloud.

Navigate to the Contact Form Service and click on the tab Service Secrets. Click on New Secret and add a secret that contains the Slack Bot token obtained earlier.

Contact form and CAPTCHA backend in Open Source Cloud

Create a contact form service by pressing the Create service button.

Contact form and CAPTCHA backend in Open Source Cloud

Give the service a name and select slack in the transport dropdown. Provide the service secret holding the token and enter the ID of the channel where the message should be posted.

Contact form and CAPTCHA backend in Open Source Cloud

When the service is up and running you can copy and save the URL to the contact form service.

Contact form example in React

Develop your contact form in your frontend application which in React could look something like this. Replace BACKEND-SERVICE-URL with the URL acquired above.


'use client';
import { Input, Textarea } from '@nextui-org/react';

export default function Page() {
  const handleSubmit = (formData: any) => {
    fetch(new URL('BACKEND-SERVICE-URL/contact'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams(formData).toString()
    });
  };

  return (
    
Nach dem Login kopieren
) }

When the form is submitted you should now get a message in your Slack channel. To prevent spam-bots to misuse this form we need to add a CAPTCHA. CAPTCHA is an acronym that stands for "Completely Automated Public Turing test to tell Computers and Humans Apart."

To provide this functionality we will use an open source CAPTCHA backend service to generate and verify CAPTCHA. How it works is that you generate a CAPTCHA image that displays a text. Then you ask the user (human) to provide the text that he or she sees and the backend will verify that this was the text shown in the image.

Create CAPTCHA backend service in Open Source Cloud

Navigate to the CAPTCHA Service and click on the button Create service.

Contact form and CAPTCHA backend in Open Source Cloud

Give the service a name and once the service has started copy the URL to the service. Replace CAPTCHA-SVC-URL below with this URL.

Add CAPTCHA verification to your form

Extend the form we created above with the following code.


<p>'use client';<br>
import { Input, Textarea } from '@nextui-org/react'; </p>

<p>export default function Page() {<br>
  const [captchaSvg, setKontaktformular und CAPTCHA-Backend in Open Source CloudSvg] = useState<string null>(null);<br>
  const [captchaId, setKontaktformular und CAPTCHA-Backend in Open Source CloudId] = useState('');<br>
  const [captcha, setKontaktformular und CAPTCHA-Backend in Open Source Cloud] = useState('');<br>
  const [invalidKontaktformular und CAPTCHA-Backend in Open Source Cloud, setInvalidKontaktformular und CAPTCHA-Backend in Open Source Cloud] = useState(true);</string></p>

<p>useEffect(() => {<br>
    fetch(new URL('/captcha', 'CAPTCHA-SVC-URL'))<br>
      .then((response) => response.json())<br>
      .then((data) => {<br>
        setKontaktformular und CAPTCHA-Backend in Open Source CloudSvg(data.svg);<br>
        setKontaktformular und CAPTCHA-Backend in Open Source CloudId(data.id);<br>
      });<br>
  }, []);</p>

<p>const onKontaktformular und CAPTCHA-Backend in Open Source CloudChange = (value: string) => {<br>
    setKontaktformular und CAPTCHA-Backend in Open Source Cloud(value);<br>
    fetch(new URL('CAPTCHA-SVC-URL/verify/' + captchaId + '/' + value))<br>
      .then((response) => {<br>
        setInvalidKontaktformular und CAPTCHA-Backend in Open Source Cloud(!response.ok);<br>
      })<br>
      .catch(() => {<br>
        setInvalidKontaktformular und CAPTCHA-Backend in Open Source Cloud(true);<br>
      });<br>
  };</p>

<p>const handleSubmit = (formData: any) => {<br>
    fetch(new URL('BACKEND-SERVICE-URL/contact'), {<br>
      method: 'POST',<br>
      headers: {<br>
        'Content-Type': 'application/x-www-form-urlencoded'<br>
      },<br>
      body: new URLSearchParams(formData).toString()<br>
    });<br>
  };</p>

<p>return (<br>
    </p>
Nach dem Login kopieren





)
}




Conclusion

This was an example on how you can add to your web application a contact form with CAPTCHA verification that posts to a Slack channel without having to build or host your own backend services for it.

What is Open Source Cloud?

Open Source Cloud reduces the barrier to get started using open source and reduces the barrier for creators to make their software available as a service. Read more about why we developed it in this post.

Das obige ist der detaillierte Inhalt vonKontaktformular und CAPTCHA-Backend in Open Source Cloud. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage