captcha example usage
The security.captcha SDK is pretty straightforward to use, but there are some considerations you need to keep in mind when implementing it.
How are you rendering the WebApp and the captcha widget?
If you use server or client rendering some different approaches apply. A simple server rendered for is very simple. If the captcha is added to the dom later on on the client, a couple of more steps are required.
Server rendered form
This example assumes a standard React rendering approach, render the compontent server side, and hydrate on the client. Since the captcha is immedietly loaded into the dom. Initiaton of the captcha widget will be managed by default by sitevision.
Example below shows an example with an intecepted request, and sending it with a call through requester. If you want to use a <form action="... approach instead, this is of course also available.
Client rendered form
For a client rendered form we have a couple of minor things to take into account. Since the captcha widget will be inserted into the DOM after the page is loaded, we need to ensure the widgets are initiated ourselves. At the moment we support Friendly Captcha and Google reCAPTCHA, to ensure both work with your solution we need to make a couple of adjustments to initiate these.
The Google reCAPTCHA widgets contains script elements which needs to run. The Friendly Captcha needs to be initiated by a function call. You do not need to know which one is used in the WebApp, it's completely fine to handle both approaches to make a general and dynamic solution.
Here's an example:
Manage captcha status
Above examples simply submits the form on a user action wether the captcha is solved or not. It is however possible to manage the user interface in the WebApp based on the current captcha status. For example, enable submit when the captcha is solved, or display a special error message on an error. To manage this you can either subscribe to an event, or manually check the status with a function call.
svCaptcha:stateChanged event
To get live status updates for captcha widgets. Subscribe to the svCaptcha:stateChanged event utilizsing the events.on SDK.
security.captcha.getState(container)
If you prefer to manually check the captcha state instead of utilising the event. The getState function is also available. Both the event and the function exposes the same data.