Logo

dev-resources.site

for different kinds of informations.

hCaptcha, a bot detection tool, usage in Supabase and Chatwoot

Published at
12/27/2024
Categories
opensource
supabase
hcaptcha
Author
ramunarasinga-11
Categories
3 categories in total
opensource
open
supabase
open
hcaptcha
open
Author
16 person written this
ramunarasinga-11
open
hCaptcha, a bot detection tool, usage in Supabase and Chatwoot

In this article, you will learn what is hCaptcha and its usage in Open Source projects such as Supabase and Chatwoot.

Image description

What is hCaptcha?

hCaptcha helps you detect bots. when you have a public facing form such as signin/signup, you could have bots spamming your systems. This is where hCaptcha comes into picture, it provides a captcha for a user to solve the puzzle so bots are not allowed to sign up.

Link to official website — https://www.hcaptcha.com/

hCaptcha docs

hCaptcha docs is built using Docusaurus and their developer guide provides a vanilla example, but there’s framework specific examples provided as well.

hCaptcha usage in Supabase

In Supabase’s SignInForm.tsx, at line 124, you will find the below code:

<div className="self-center">
 <HCaptcha
   ref={captchaRef}
   sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
   size="invisible"
   onVerify={(token) => {
     setCaptchaToken(token)
   }}
   onExpire={() => {
     setCaptchaToken(null)
   }}
 />
</div>
Enter fullscreen mode Exit fullscreen mode

hCaptcha is imported from react-hcaptcha.

import HCaptcha from '@hcaptcha/react-hcaptcha'
Enter fullscreen mode Exit fullscreen mode

In the onSignin function, hCaptcha is processed as shown below:

const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const captchaRef = useRef<HCaptcha>(null)

let token = captchaToken

if (!token) {
 const captchaResponse = await captchaRef.current?.execute({ async: true })
 token = captchaResponse?.response ?? null
}

const { error } = await auth.signInWithPassword({
 email,
 password,
 options: { captchaToken: token ?? undefined },
})
Enter fullscreen mode Exit fullscreen mode

hCaptcha usage in Chatwoot

In the Chatwoot Signup form, you will find the below code at line 186:

<div v-if="globalConfig.hCaptchaSiteKey" class="mb-3">
  <VueHcaptcha
    ref="hCaptcha"
    :class="{ error: !hasAValidCaptcha && didCaptchaReset }"
    :sitekey="globalConfig.hCaptchaSiteKey"
    @verify="onRecaptchaVerified"
  />
  <span
    v-if="!hasAValidCaptcha && didCaptchaReset"
    class="text-xs text-red-400"
  >
    {{ $t('SET_NEW_PASSWORD.CAPTCHA.ERROR') }}
  </span>
</div>
Enter fullscreen mode Exit fullscreen mode

This is in Vue.js, based on @verify=”onRecaptchaVerified”, we should be looking for a function named onRecaptchaVerified. In the methods, hcaptcha related functions are found as shown below:

methods: {
    async submit() {
      this.v$.$touch();
      if (this.v$.$invalid) {
        this.resetCaptcha();
        return;
      }
      this.isSignupInProgress = true;
      try {
        await register(this.credentials);
        window.location = DEFAULT_REDIRECT_URL;
      } catch (error) {
        let errorMessage =
          error?.message || this.$t('REGISTER.API.ERROR_MESSAGE');
        this.resetCaptcha();
        useAlert(errorMessage);
      } finally {
        this.isSignupInProgress = false;
      }
    },
    onRecaptchaVerified(token) {
      this.credentials.hCaptchaClientResponse = token;
      this.didCaptchaReset = false;
    },
    resetCaptcha() {
      if (!this.globalConfig.hCaptchaSiteKey) {
        return;
      }
      this.$refs.hCaptcha.reset();
      this.credentials.hCaptchaClientResponse = '';
      this.didCaptchaReset = true;
    },
  },
};
Enter fullscreen mode Exit fullscreen mode

About us:

  1. We study large open source projects and provide free architectural guides.

  2. We have developed free, reusable Components, built with tailwind, that you can use in your project.

  3. We analyse open-source code and provide courses so you can learn various advanced techniques and improve your skills. Get our courses.

  4. Subscribe to our Youtube Channel to get the insights from the open-source projects, where we review code snippets.

References:

  1. https://github.com/supabase/supabase/blob/master/apps/studio/components/interfaces/SignIn/SignInForm.tsx#L22

  2. https://github.com/chatwoot/chatwoot/blob/4fd9bddb9de08666f7b65ba4b3509f02080ea62d/app/javascript/v3/views/auth/signup/components/Signup/Form.vue#L8

  3. https://www.hcaptcha.com/

  4. https://docs.hcaptcha.com/

  5. https://docusaurus.io/

  6. https://github.com/supabase/supabase/blob/master/apps/studio/components/interfaces/SignIn/SignInForm.tsx#L124

  7. https://github.com/supabase/supabase/blob/master/apps/studio/components/interfaces/SignIn/SignInForm.tsx#L30

  8. https://github.com/chatwoot/chatwoot/blob/4fd9bddb9de08666f7b65ba4b3509f02080ea62d/app/javascript/v3/views/auth/signup/components/Signup/Form.vue#L186

supabase Article's
30 articles in total
Favicon
Building Production-Grade Web Applications with Supabase – Part 1
Favicon
Tracing and Metrics in supabase/storage
Favicon
Understanding Storage backends in supabase/storage
Favicon
Auth in Supabase storage
Favicon
Overview of supabase storage repository
Favicon
Harnessing the Power of Self-Hosted Supabase on Coolify: A Complete Guide to Server Setup and OAuth Providers Integration
Favicon
checa como se hace una conexión entre nextjs y la autenticación de supabase
Favicon
Deploying an Existing Express API + Prisma + Supabase Project to Vercel
Favicon
Custom schema specific Supabase Server Component clients in Grida Form workspace
Favicon
Custom schema specific Supabase Client Component clients in Grida Form workspace
Favicon
Introducing Supabase Client Playground: The Ultimate Tool for Streamlined Query Testing
Favicon
hCaptcha, a bot detection tool, usage in Supabase and Chatwoot
Favicon
Securing Client-Side Routes in Next.js with Supabase
Favicon
How to Transfer PostgreSQL Database from Local to Supabase on macOS
Favicon
Supabase | My Way of Designing & Managing DB
Favicon
What does Supabase use for its Authentication?
Favicon
É seguro guardar dados do usuário no localStorage?
Favicon
Supabase RLS Alternative
Favicon
Build Queue Worker using Supabase Cron, Queue and Edge Function
Favicon
Supabase Just Got More Powerful: Queue, Cron, and Background Tasks in Edge Functions
Favicon
Comparison of the middleware implementation between Supabase Auth documentation and the nextjs-stripe-supabase.
Favicon
How Supabase implemented micro-frontends using Multi Zones in Next.js
Favicon
Unlocking User Data: Building a Secure Supabase Edge Function
Favicon
Usecase: TumbleLog
Favicon
Supabase Edge Functions
Favicon
Setup Astro With Supabase and Prisma
Favicon
We launched SupaCharts! Visualize Beautiful Charts from your Supabase Data.
Favicon
A Free Minimalistic SaaS Starter Kit for Fast MVP Building
Favicon
supabase注册并创建项目和添加数据表
Favicon
Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide

Featured ones: