Showing captcha on frontend request
-
Install VK Капча SDK.
-
Add the import of the
CaptchaWidgetclass and theCaptchaPublicEventsobject to the frontend:npm, yarn, pnpmJS scriptimport { CaptchaPublicEvents, CaptchaWidget } from "@vkid/captcha"; -
Create an instance of the captcha widget:
const captchaWidget = new CaptchaWidget(); -
Subscribe to events using the SDK method captchaWidget.on().
Invisible captchaEmbedded captchaPopup windowIf the service considers the user suspicious, the invisible captcha will be automatically replaced with a visible one. In this case, VK Капча SDK will send the
captcha: challenge visibleevent, and after the captcha is completed — thecaptcha: challenge hiddenevent.Example:
captchaWidget.on(CaptchaPublicEvents.SUCCESS, onSuccess);captchaWidget.on(CaptchaPublicEvents.CHALLENGE_VISIBLE, onChallengeVisible);captchaWidget.on(CaptchaPublicEvents.CHALLENGE_HIDDEN, onChallengeHidden);captchaWidget.on(CaptchaPublicEvents.TOKEN_EXPIRED, onTokenExpired); -
Get the captcha widget launch link from the backend.
-
Mount the captcha on the page. Use the SDK method captchaWidget.render().
Invisible captchaEmbedded captchaPopup windowSet the
invisibleparameter totrue.Example:
await captchaWidget.render({link,lang: 'ru',invisible: true,notice: { position: 'bottom-left', draggable: true },});For the invisible captcha, VK Капча SDK will display an automatic data processing notification to the user.
-
Call the SDK method captchaWidget.execute() on the target action.
The method will return a promise object with the captcha completion result: a successful captcha completion token if the user passed the captcha, or an error.
-
Process the captcha completion result:
- If the promise object contains a successful captcha completion token, send it to the backend for validation.
- Determine what will happen in the frontend if the promise object returns an error. Error codes are described in the article SDK reference for web applications.
-
(Optional) For a single-page application (SPA, Single Page Application), where screens change without reloading the page, use the SDK method captchaWidget.destroy() to remove the previous captcha from the page.
TypeScript code examples:
import { CaptchaPublicEvents, CaptchaWidget } from '@vkid/captcha';const captchaWidget = new CaptchaWidget();// Set up subscriptions before mounting to avoid losing early eventscaptchaWidget.on(CaptchaPublicEvents.SUCCESS, onSuccess);captchaWidget.on(CaptchaPublicEvents.CHALLENGE_VISIBLE, onChallengeVisible);captchaWidget.on(CaptchaPublicEvents.CHALLENGE_HIDDEN, onChallengeHidden);captchaWidget.on(CaptchaPublicEvents.TOKEN_EXPIRED, onTokenExpired);// Mounting the captcha on the pageconst { link } = await api.post('/api/captcha/session');await captchaWidget.render({link,lang: 'ru',invisible: true,notice: { position: 'bottom-left', draggable: true },});// Target actionasync function onSubmit({ login, password }: Credentials) {const captchaToken = await captchaWidget.execute();await api.post('/api/login', { login, password, captchaToken });}