Authorizes the CAPTCHA token.
Following CAPTCHA verification on the client side, you must authorize
the generated CAPTCHA token in the backend. authorize()
checks if the token
is valid, making sure it was not tampered with or timed out.
The authorize()
function returns a Promise that resolves to a Success
object when the token is authorized and to an Error
object when authorization fails.
To understand how authorize()
is used in a typical CAPTCHA validation lifecycle,
click here.
If CAPTCHA token authorization fails, an error message containing a status code is returned. The following table lists the possible HTTP error status codes, based on RFC 2616:
Status Code | Name | Description |
---|---|---|
400 | Bad Request | The request could not be understood by the server. This could occur for a number of reasons, such as: The request was sent without a token. The token is invalid. The token has timed out. |
401 | Unauthenticated | No user identity found in passed request. |
500 | Internal Server Error | The server encountered an unexpected condition, such as a missing or invalid private CAPTCHA key. |
503 | Unavailable | The service is unavailable due to one of the following: Throttled error: Server overload due to more than the allowed requests in a given time frame. Request failed: No response following 10 retries with a 1-second interval. |
function authorize(token: string): Promise<SuccessReport>;
The CAPTCHA token to authorize.
This example demonstrates how to use reCAPTCHA to protect a data insertion. We use a text input for the data, a reCAPTCHA element, and a submit button. The submit button is disabled until the CAPTCHA is verified and a token is generated. Clicking the submit button triggers backend authorization of the token. If authorization is successful, the data is inserted into the collection.
/************************************
* Backend code - submitHandler.web.js *
************************************/
import { Permissions, webMethod } from "wix-web-module";
import wixCaptchaBackend from "wix-captcha-backend";
import wixData from "wix-data";
export const processSubmission = webMethod(
Permissions.Anyone,
(submitRequestData) => {
return wixCaptchaBackend
.authorize(submitRequestData.token)
.then(() => {
return wixData
.insert("MyCollection", submitRequestData.data)
.then(() => ({ type: "success" }))
.catch((error) => ({
type: "insertion error",
message: "Error: collection insertion failed: " + error,
}));
})
.catch((error) => ({
type: "authorization error",
message: "Error: CAPTCHA authorization failed: " + error,
}));
},
);
/********************
* Client-side code *
********************/
import { processSubmission } from "backend/submitHandler.web";
$w.onReady(function () {
// When user clicks submit button
$w("#submitDataButton").onClick(() => {
let submitRequestData = {
token: $w("#myCaptcha").token,
data: $w("#myInput").value,
};
processSubmission(submitRequestData) // Call backend function
.then((response) => {
// Display a different message depending on response from backend function
switch (response.type) {
case "success":
$w("#messageText").text = "Data successfully submitted";
break;
case "authorization error":
$w("#messageText").text =
"CAPTCHA authorization failed. Redo the CAPTCHA challenge.";
break;
case "insertion error":
$w("#messageText").text =
"Database error. Redo the CAPTCHA challenge.";
break;
}
$w("#myCaptcha").reset();
$w("#submitDataButton").disable();
$w("#messageText").show();
});
});
// Error handler
$w("#myCaptcha").onError(() => {
$w("#messageText").text =
"The reCAPTCHA element lost connection with the CAPTCHA provider. Try again later.";
$w("#messageText")
.show()
.then(() => {
$w("#messageText").hide("fade", { delay: 10000 });
});
});
// Verification handler
$w("#myCaptcha").onVerified(() => {
$w("#submitDataButton").enable();
$w("#messageText").hide();
});
// Timeout handler
$w("#myCaptcha").onTimeout(() => {
$w("#submitDataButton").disable();
$w("#messageText").text =
"The CAPTCHA has timed out. Please redo the CAPTCHA challenge.";
$w("#messageText").show();
});
});
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.