Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Google reCAPTCHA v2 implementation for Svelte SPA, SSR and sveltekit static sites.

License

NotificationsYou must be signed in to change notification settings

basaran/svelte-recaptcha-v2

Repository files navigation

svelte-recaptcha-v2

svelte-recaptcha-v2

Google reCAPTCHA v2 implementation for Svelte SPA, SSR and sveltekit static sites.

Features

  • svelte server side rendering (SSR) friendly.
  • works with sveltekit SPA, SSR and static site adapters.
  • easy integration with third party form validation libraries.
  • fail-safe loader to inject recaptcha.
  • invisible recaptcha or checkbox recaptcha support.
  • event model for intercepting various recaptcha states.
  • handle all your form logic in a single submit handler.
  • proper DOM cleanup (deletes recaptcha completely)
  • documented, debug.js friendly source code.
  • typescript definitions are included for LSP.

Demonstration

svelte-recaptcha-v2 demo

Getting Started

# install as a development dependencypnpm install -D svelte-recaptcha-v2

Basic Usage

Import the library onto your template and update your google key:

import{Recaptcha,recaptcha,observer}from"svelte-recaptcha-v2";/* │Recaptcha: svelte <Recaptcha> component. │recaptcha: google method, gives you recaptcha.execute(). │observer: allows you to track captcha state across components. */constgoogleRecaptchaSiteKey="replace_with_yours";

In your form, add the component:

<Recaptchasitekey={googleRecaptchaSiteKey}badge={"top"}size={"invisible"}on:success={onCaptchaSuccess}on:error={onCaptchaError}on:expired={onCaptchaExpire}on:close={onCaptchaClose}on:ready={onCaptchaReady} />

Setup your event handlers:

constonCaptchaReady=(event)=>{console.log("recaptcha init has completed.")/*     │You can enable your form button here.     */};constonCaptchaSuccess=(event)=>{userTracker.resolve(event);console.log("token received: "+event.detail.token);/*     │If using checkbox method, you can attach your     │form logic here, or dispatch your custom event.     */};constonCaptchaError=(event)=>{console.log("recaptcha init has failed.");/*     │Usually due to incorrect siteKey.     |Make sure you have the correct siteKey..     */};constonCaptchaExpire=(event)=>{console.log("recaptcha api has expired");/*     │Normally, you wouldn't need to do anything.     │Recaptcha should reinit itself automatically.     */};constonCaptchaOpen=(event)=>{console.log("google decided to challange the user");/*     │This fires when the puzzle frame pops.     */};constonCaptchaClose=(event)=>{console.log("google decided to challange the user");/*     │This fires when the puzzle frame closes.     │Usually happens when the user clicks outside     |the modal frame.     */};

Update your form handler:

constsubmitHandler=async()=>{console.log("launching recaptcha");recaptcha.execute();console.log("pending for google response");constevent=awaitPromise.resolve(observer);constrecaptchaToken=event.detail?.token ?event.detail.token :false;if(!recaptchaToken){console.log("recaptcha is NOT OK");returnfalse;}console.log("token retrieved",recaptchaToken);};

Debugging

If you would like to enable client side debugging, add{Recaptcha} value to your localStorageDEBUG key.

Issues

If any trouble, please create an issue. PRs are most welcome.

Todo

  • [] add recipes for validation libraries (felte, stock etc)
  • [] look into mutation observers

About

Google reCAPTCHA v2 implementation for Svelte SPA, SSR and sveltekit static sites.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp